Move into nested docroot
This commit is contained in:
parent
83a0d3a149
commit
c8b70abde9
13405 changed files with 0 additions and 0 deletions
|
@ -0,0 +1,93 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\telephone\Plugin\Field\FieldFormatter;
|
||||
|
||||
use Drupal\Core\Field\FormatterBase;
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
use Drupal\Core\Url;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'telephone_link' formatter.
|
||||
*
|
||||
* @FieldFormatter(
|
||||
* id = "telephone_link",
|
||||
* label = @Translation("Telephone link"),
|
||||
* field_types = {
|
||||
* "telephone"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class TelephoneLinkFormatter extends FormatterBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
'title' => '',
|
||||
) + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$elements['title'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Title to replace basic numeric telephone number display'),
|
||||
'#default_value' => $this->getSetting('title'),
|
||||
);
|
||||
|
||||
return $elements;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsSummary() {
|
||||
$summary = array();
|
||||
$settings = $this->getSettings();
|
||||
|
||||
if (!empty($settings['title'])) {
|
||||
$summary[] = t('Link using text: @title', array('@title' => $settings['title']));
|
||||
}
|
||||
else {
|
||||
$summary[] = t('Link using provided telephone number.');
|
||||
}
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function viewElements(FieldItemListInterface $items, $langcode) {
|
||||
$element = array();
|
||||
$title_setting = $this->getSetting('title');
|
||||
|
||||
foreach ($items as $delta => $item) {
|
||||
// Render each element as link.
|
||||
$element[$delta] = array(
|
||||
'#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(preg_replace('/\s+/', '', $item->value))),
|
||||
'#options' => array('external' => TRUE),
|
||||
);
|
||||
|
||||
if (!empty($item->_attributes)) {
|
||||
$element[$delta]['#options'] += array('attributes' => array());
|
||||
$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,85 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\telephone\Plugin\Field\FieldType;
|
||||
|
||||
use Drupal\Core\Field\FieldDefinitionInterface;
|
||||
use Drupal\Core\Field\FieldItemBase;
|
||||
use Drupal\Core\Field\FieldStorageDefinitionInterface;
|
||||
use Drupal\Core\TypedData\DataDefinition;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'telephone' field type.
|
||||
*
|
||||
* @FieldType(
|
||||
* id = "telephone",
|
||||
* label = @Translation("Telephone number"),
|
||||
* description = @Translation("This field stores a telephone number in the database."),
|
||||
* category = @Translation("Number"),
|
||||
* default_widget = "telephone_default",
|
||||
* default_formatter = "basic_string"
|
||||
* )
|
||||
*/
|
||||
class TelephoneItem extends FieldItemBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function schema(FieldStorageDefinitionInterface $field_definition) {
|
||||
return array(
|
||||
'columns' => array(
|
||||
'value' => array(
|
||||
'type' => 'varchar',
|
||||
'length' => 256,
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {
|
||||
$properties['value'] = DataDefinition::create('string')
|
||||
->setLabel(t('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();
|
||||
|
||||
$max_length = 256;
|
||||
$constraints[] = $constraint_manager->create('ComplexData', array(
|
||||
'value' => array(
|
||||
'Length' => array(
|
||||
'max' => $max_length,
|
||||
'maxMessage' => t('%name: the telephone number may not be longer than @max characters.', array('%name' => $this->getFieldDefinition()->getLabel(), '@max' => $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,73 @@
|
|||
<?php
|
||||
|
||||
namespace Drupal\telephone\Plugin\Field\FieldWidget;
|
||||
|
||||
use Drupal\Core\Field\FieldItemListInterface;
|
||||
use Drupal\Core\Field\WidgetBase;
|
||||
use Drupal\Core\Form\FormStateInterface;
|
||||
|
||||
/**
|
||||
* Plugin implementation of the 'telephone_default' widget.
|
||||
*
|
||||
* @FieldWidget(
|
||||
* id = "telephone_default",
|
||||
* label = @Translation("Telephone number"),
|
||||
* field_types = {
|
||||
* "telephone"
|
||||
* }
|
||||
* )
|
||||
*/
|
||||
class TelephoneDefaultWidget extends WidgetBase {
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static function defaultSettings() {
|
||||
return array(
|
||||
'placeholder' => '',
|
||||
) + parent::defaultSettings();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function settingsForm(array $form, FormStateInterface $form_state) {
|
||||
$element['placeholder'] = array(
|
||||
'#type' => 'textfield',
|
||||
'#title' => t('Placeholder'),
|
||||
'#default_value' => $this->getSetting('placeholder'),
|
||||
'#description' => 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 = array();
|
||||
|
||||
$placeholder = $this->getSetting('placeholder');
|
||||
if (!empty($placeholder)) {
|
||||
$summary[] = t('Placeholder: @placeholder', array('@placeholder' => $placeholder));
|
||||
}
|
||||
else {
|
||||
$summary[] = t('No placeholder');
|
||||
}
|
||||
|
||||
return $summary;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
|
||||
$element['value'] = $element + array(
|
||||
'#type' => 'tel',
|
||||
'#default_value' => isset($items[$delta]->value) ? $items[$delta]->value : NULL,
|
||||
'#placeholder' => $this->getSetting('placeholder'),
|
||||
);
|
||||
return $element;
|
||||
}
|
||||
|
||||
}
|
Reference in a new issue