Initial Drupal 11 with DDEV setup
This commit is contained in:
70
web/core/modules/telephone/src/Hook/TelephoneHooks.php
Normal file
70
web/core/modules/telephone/src/Hook/TelephoneHooks.php
Normal file
@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\telephone\Hook;
|
||||
|
||||
use Drupal\Core\Field\FieldTypeCategoryManagerInterface;
|
||||
use Drupal\Core\StringTranslation\StringTranslationTrait;
|
||||
use Drupal\Core\Url;
|
||||
use Drupal\Core\Routing\RouteMatchInterface;
|
||||
use Drupal\Core\Hook\Attribute\Hook;
|
||||
|
||||
/**
|
||||
* Hook implementations for telephone.
|
||||
*/
|
||||
class TelephoneHooks {
|
||||
|
||||
use StringTranslationTrait;
|
||||
|
||||
/**
|
||||
* Implements hook_help().
|
||||
*/
|
||||
#[Hook('help')]
|
||||
public function help($route_name, RouteMatchInterface $route_match): ?string {
|
||||
switch ($route_name) {
|
||||
case 'help.page.telephone':
|
||||
$output = '';
|
||||
$output .= '<h2>' . $this->t('About') . '</h2>';
|
||||
$output .= '<p>' . $this->t('The Telephone module allows you to create fields that contain telephone numbers. See the <a href=":field">Field module help</a> and the <a href=":field_ui">Field UI help</a> pages for general information on fields and how to create and manage them. For more information, see the <a href=":telephone_documentation">online documentation for the Telephone module</a>.', [
|
||||
':field' => Url::fromRoute('help.page', [
|
||||
'name' => 'field',
|
||||
])->toString(),
|
||||
':field_ui' => \Drupal::moduleHandler()->moduleExists('field_ui') ? Url::fromRoute('help.page', [
|
||||
'name' => 'field_ui',
|
||||
])->toString() : '#',
|
||||
':telephone_documentation' => 'https://www.drupal.org/documentation/modules/telephone',
|
||||
]) . '</p>';
|
||||
$output .= '<h2>' . $this->t('Uses') . '</h2>';
|
||||
$output .= '<dl>';
|
||||
$output .= '<dt>' . $this->t('Managing and displaying telephone fields') . '</dt>';
|
||||
$output .= '<dd>' . $this->t('The <em>settings</em> and the <em>display</em> of the telephone field can be configured separately. See the <a href=":field_ui">Field UI help</a> for more information on how to manage fields and their display.', [
|
||||
':field_ui' => \Drupal::moduleHandler()->moduleExists('field_ui') ? Url::fromRoute('help.page', [
|
||||
'name' => 'field_ui',
|
||||
])->toString() : '#',
|
||||
]) . '</dd>';
|
||||
$output .= '<dt>' . $this->t('Displaying telephone numbers as links') . '</dt>';
|
||||
$output .= '<dd>' . $this->t('Telephone numbers can be displayed as links with the scheme name <em>tel:</em> by choosing the <em>Telephone</em> display format on the <em>Manage display</em> page. Any spaces will be stripped out of the link text. This semantic markup improves the user experience on mobile and assistive technology devices.') . '</dd>';
|
||||
$output .= '</dl>';
|
||||
return $output;
|
||||
}
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_formatter_info_alter().
|
||||
*/
|
||||
#[Hook('field_formatter_info_alter')]
|
||||
public function fieldFormatterInfoAlter(&$info): void {
|
||||
$info['string']['field_types'][] = 'telephone';
|
||||
}
|
||||
|
||||
/**
|
||||
* Implements hook_field_type_category_info_alter().
|
||||
*/
|
||||
#[Hook('field_type_category_info_alter')]
|
||||
public function fieldTypeCategoryInfoAlter(&$definitions): void {
|
||||
// The `telephone` field type belongs in the `general` category, so the
|
||||
// libraries need to be attached using an alter hook.
|
||||
$definitions[FieldTypeCategoryManagerInterface::FALLBACK_CATEGORY]['libraries'][] = 'telephone/drupal.telephone-icon';
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,109 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\telephone\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Field\Attribute\FieldFormatter;
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
use Drupal\Core\Url;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'telephone_link' formatter.
|
||||
*/
|
||||
#[FieldFormatter(
|
||||
id: 'telephone_link',
|
||||
label: new TranslatableMarkup('Telephone link'),
|
||||
field_types: [
|
||||
'telephone',
|
||||
],
|
||||
)]
|
||||
class TelephoneLinkFormatter extends FormatterBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return [
|
||||
'title' => '',
|
||||
] + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$elements['title'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Title to replace basic numeric telephone number display'),
|
||||
'#default_value' => $this->getSetting('title'),
|
||||
];
|
||||
|
||||
return $elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = [];
|
||||
$settings = $this->getSettings();
|
||||
|
||||
if (!empty($settings['title'])) {
|
||||
$summary[] = $this->t('Link using text: @title', ['@title' => $settings['title']]);
|
||||
}
|
||||
else {
|
||||
$summary[] = $this->t('Link using provided telephone number.');
|
||||
}
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
$element = [];
|
||||
$title_setting = $this->getSetting('title');
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
// If the telephone number is 5 or less digits, parse_url() will think
|
||||
// it's a port number rather than a phone number which causes the link
|
||||
// formatter to throw an InvalidArgumentException. Avoid this by inserting
|
||||
// a dash (-) after the first digit - RFC 3966 defines the dash as a
|
||||
// visual separator character and so will be removed before the phone
|
||||
// number is used. See https://bugs.php.net/bug.php?id=70588 for more.
|
||||
// While the bug states this only applies to numbers <= 65535, a 5 digit
|
||||
// number greater than 65535 will cause parse_url() to return FALSE so
|
||||
// we need the work around on any 5 digit (or less) number.
|
||||
// First we strip whitespace so we're counting actual digits.
|
||||
$phone_number = preg_replace('/\s+/', '', $item->value);
|
||||
if (strlen($phone_number) <= 5) {
|
||||
$phone_number = substr_replace($phone_number, '-', 1, 0);
|
||||
}
|
||||
|
||||
// Render each element as link.
|
||||
$element[$delta] = [
|
||||
'#type' => 'link',
|
||||
// Use custom title if available, otherwise use the telephone number
|
||||
// itself as title.
|
||||
'#title' => $title_setting ?: $item->value,
|
||||
// Prepend 'tel:' to the telephone number.
|
||||
'#url' => Url::fromUri('tel:' . rawurlencode($phone_number)),
|
||||
'#options' => ['external' => TRUE],
|
||||
];
|
||||
|
||||
if (!empty($item->_attributes)) {
|
||||
$element[$delta]['#options'] += ['attributes' => []];
|
||||
$element[$delta]['#options']['attributes'] += $item->_attributes;
|
||||
// Unset field item attributes since they have been included in the
|
||||
// formatter output and should not be rendered in the field template.
|
||||
unset($item->_attributes);
|
||||
}
|
||||
}
|
||||
|
||||
return $element;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,89 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\telephone\Plugin\Field\FieldType;
|
||||
|
||||
use Drupal\Core\Field\Attribute\FieldType;
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Field\FieldItemBase;
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
use Drupal\Core\TypedData\DataDefinition;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'telephone' field type.
|
||||
*/
|
||||
#[FieldType(
|
||||
id: "telephone",
|
||||
label: new TranslatableMarkup("Telephone number"),
|
||||
description: new TranslatableMarkup("This field stores a telephone number."),
|
||||
default_widget: "telephone_default",
|
||||
default_formatter: "basic_string"
|
||||
)]
|
||||
class TelephoneItem extends FieldItemBase {
|
||||
|
||||
/**
|
||||
* The maximum length for a telephone value.
|
||||
*/
|
||||
const MAX_LENGTH = 256;
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function schema(FieldStorageDefinitionInterface $field_definition) {
|
||||
return [
|
||||
'columns' => [
|
||||
'value' => [
|
||||
'type' => 'varchar',
|
||||
'length' => self::MAX_LENGTH,
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
|
||||
$properties['value'] = DataDefinition::create('string')
|
||||
->setLabel(new TranslatableMarkup('Telephone number'))
|
||||
->setRequired(TRUE);
|
||||
|
||||
return $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function isEmpty() {
|
||||
$value = $this->get('value')->getValue();
|
||||
return $value === NULL || $value === '';
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getConstraints() {
|
||||
$constraint_manager = \Drupal::typedDataManager()->getValidationConstraintManager();
|
||||
$constraints = parent::getConstraints();
|
||||
|
||||
$constraints[] = $constraint_manager->create('ComplexData', [
|
||||
'value' => [
|
||||
'Length' => [
|
||||
'max' => self::MAX_LENGTH,
|
||||
'maxMessage' => $this->t('%name: the telephone number may not be longer than @max characters.', ['%name' => $this->getFieldDefinition()->getLabel(), '@max' => self::MAX_LENGTH]),
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
return $constraints;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function generateSampleValue(FieldDefinitionInterface $field_definition) {
|
||||
$values['value'] = rand(pow(10, 8), pow(10, 9) - 1);
|
||||
return $values;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,74 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\telephone\Plugin\Field\FieldWidget;
|
||||
|
||||
use Drupal\Core\Field\Attribute\FieldWidget;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\WidgetBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\StringTranslation\TranslatableMarkup;
|
||||
use Drupal\telephone\Plugin\Field\FieldType\TelephoneItem;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'telephone_default' widget.
|
||||
*/
|
||||
#[FieldWidget(
|
||||
id: 'telephone_default',
|
||||
label: new TranslatableMarkup('Telephone number'),
|
||||
field_types: ['telephone'],
|
||||
)]
|
||||
class TelephoneDefaultWidget extends WidgetBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return [
|
||||
'placeholder' => '',
|
||||
] + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$element['placeholder'] = [
|
||||
'#type' => 'textfield',
|
||||
'#title' => $this->t('Placeholder'),
|
||||
'#default_value' => $this->getSetting('placeholder'),
|
||||
'#description' => $this->t('Text that will be shown inside the field until a value is entered. This hint is usually a sample value or a brief description of the expected format.'),
|
||||
];
|
||||
return $element;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = [];
|
||||
|
||||
$placeholder = $this->getSetting('placeholder');
|
||||
if (!empty($placeholder)) {
|
||||
$summary[] = $this->t('Placeholder: @placeholder', ['@placeholder' => $placeholder]);
|
||||
}
|
||||
else {
|
||||
$summary[] = $this->t('No placeholder');
|
||||
}
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
|
||||
$element['value'] = $element + [
|
||||
'#type' => 'tel',
|
||||
'#default_value' => $items[$delta]->value ?? NULL,
|
||||
'#placeholder' => $this->getSetting('placeholder'),
|
||||
'#maxlength' => TelephoneItem::MAX_LENGTH,
|
||||
];
|
||||
return $element;
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,31 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\telephone\Plugin\migrate\field\d7;
|
||||
|
||||
use Drupal\migrate_drupal\Attribute\MigrateField;
|
||||
use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
|
||||
|
||||
/**
|
||||
* Migrate field plugin for Drupal 7 phone fields.
|
||||
*/
|
||||
#[MigrateField(
|
||||
id: 'phone',
|
||||
core: [7],
|
||||
type_map: [
|
||||
'phone' => 'telephone',
|
||||
],
|
||||
source_module: 'phone',
|
||||
destination_module: 'telephone',
|
||||
)]
|
||||
class PhoneField extends FieldPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFieldFormatterMap() {
|
||||
return [
|
||||
'phone' => 'basic_string',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
@ -0,0 +1,38 @@
|
||||
<?php
|
||||
|
||||
namespace Drupal\telephone\Plugin\migrate\field\d7;
|
||||
|
||||
use Drupal\migrate_drupal\Attribute\MigrateField;
|
||||
use Drupal\migrate_drupal\Plugin\migrate\field\FieldPluginBase;
|
||||
|
||||
/**
|
||||
* Migrate field plugin for Drupal 7 telephone fields.
|
||||
*/
|
||||
#[MigrateField(
|
||||
id: 'telephone',
|
||||
core: [7],
|
||||
source_module: 'telephone',
|
||||
destination_module: 'telephone',
|
||||
)]
|
||||
class TelephoneField extends FieldPluginBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFieldWidgetMap() {
|
||||
// The widget IDs are identical in Drupal 7 and 8, so we do not need any
|
||||
// mapping.
|
||||
return [];
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function getFieldFormatterMap() {
|
||||
return [
|
||||
'text_plain' => 'string',
|
||||
'telephone_link' => 'telephone_link',
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user