Issue #2015683 by plopesc: Convert field type to typed data plugin for telephone module .
parent
bfba7b8de9
commit
389338d5b2
|
@ -0,0 +1,92 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @file
|
||||||
|
* Contains \Drupal\telephone\Plugin\field\field_type\TelephoneItem.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace Drupal\telephone\Plugin\field\field_type;
|
||||||
|
|
||||||
|
use Drupal\Core\Entity\Annotation\FieldType;
|
||||||
|
use Drupal\Core\Annotation\Translation;
|
||||||
|
use Drupal\field\Plugin\Type\FieldType\ConfigFieldItemBase;
|
||||||
|
use Drupal\field\Plugin\Core\Entity\Field;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Plugin implementation of the 'telephone' field type.
|
||||||
|
*
|
||||||
|
* @FieldType(
|
||||||
|
* id = "telephone",
|
||||||
|
* module = "telephone",
|
||||||
|
* label = @Translation("Telephone number"),
|
||||||
|
* description = @Translation("This field stores a telephone number in the database."),
|
||||||
|
* default_widget = "telephone_default",
|
||||||
|
* default_formatter = "telephone_link"
|
||||||
|
* )
|
||||||
|
*/
|
||||||
|
class TelephoneItem extends ConfigFieldItemBase {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Definitions of the contained properties.
|
||||||
|
*
|
||||||
|
* @var array
|
||||||
|
*/
|
||||||
|
static $propertyDefinitions;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public static function schema(Field $field) {
|
||||||
|
return array(
|
||||||
|
'columns' => array(
|
||||||
|
'value' => array(
|
||||||
|
'type' => 'varchar',
|
||||||
|
'length' => 256,
|
||||||
|
'not null' => FALSE,
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getPropertyDefinitions() {
|
||||||
|
if (!isset(static::$propertyDefinitions)) {
|
||||||
|
static::$propertyDefinitions['value'] = array(
|
||||||
|
'type' => 'string',
|
||||||
|
'label' => t('Telephone number'),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
return static::$propertyDefinitions;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function isEmpty() {
|
||||||
|
$value = $this->get('value')->getValue();
|
||||||
|
return $value === NULL || $value === '';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function getConstraints() {
|
||||||
|
$constraint_manager = \Drupal::typedData()->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->getInstance()->label, '@max' => $max_length)),
|
||||||
|
)
|
||||||
|
),
|
||||||
|
));
|
||||||
|
|
||||||
|
return $constraints;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -1,38 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @file
|
|
||||||
* Contains \Drupal\telephone\Type\TelephoneItem.
|
|
||||||
*/
|
|
||||||
|
|
||||||
namespace Drupal\telephone\Type;
|
|
||||||
|
|
||||||
use Drupal\field\Plugin\field\field_type\LegacyConfigFieldItem;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Defines the 'telephone_field' entity field items.
|
|
||||||
*/
|
|
||||||
class TelephoneItem extends LegacyConfigFieldItem {
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Definitions of the contained properties.
|
|
||||||
*
|
|
||||||
* @see TelephoneItem::getPropertyDefinitions()
|
|
||||||
*
|
|
||||||
* @var array
|
|
||||||
*/
|
|
||||||
static $propertyDefinitions;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements ComplexDataInterface::getPropertyDefinitions().
|
|
||||||
*/
|
|
||||||
public function getPropertyDefinitions() {
|
|
||||||
if (!isset(static::$propertyDefinitions)) {
|
|
||||||
static::$propertyDefinitions['value'] = array(
|
|
||||||
'type' => 'string',
|
|
||||||
'label' => t('Telephone number'),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
return static::$propertyDefinitions;
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,22 +0,0 @@
|
||||||
<?php
|
|
||||||
|
|
||||||
/**
|
|
||||||
* @file
|
|
||||||
* Install, update and uninstall functions for the Telephone module.
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements hook_field_schema().
|
|
||||||
*/
|
|
||||||
function telephone_field_schema($field) {
|
|
||||||
$columns = array(
|
|
||||||
'value' => array(
|
|
||||||
'type' => 'varchar',
|
|
||||||
'length' => 256,
|
|
||||||
'not null' => FALSE,
|
|
||||||
),
|
|
||||||
);
|
|
||||||
return array(
|
|
||||||
'columns' => $columns,
|
|
||||||
);
|
|
||||||
}
|
|
|
@ -5,36 +5,15 @@
|
||||||
* Defines a simple telephone number field type.
|
* Defines a simple telephone number field type.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements hook_field_info().
|
|
||||||
*/
|
|
||||||
function telephone_field_info() {
|
|
||||||
return array(
|
|
||||||
'telephone' => array(
|
|
||||||
'label' => t('Telephone number'),
|
|
||||||
'description' => t('This field stores a telephone number in the database.'),
|
|
||||||
'default_widget' => 'telephone_default',
|
|
||||||
'default_formatter' => 'telephone_link',
|
|
||||||
'class' => 'Drupal\telephone\Type\TelephoneItem',
|
|
||||||
),
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements hook_field_info_alter().
|
* Implements hook_field_info_alter().
|
||||||
*/
|
*/
|
||||||
function telephone_field_info_alter(&$info) {
|
function telephone_field_info_alter(&$info) {
|
||||||
if (module_exists('text')) {
|
if (Drupal::moduleHandler()->moduleExists('text')) {
|
||||||
$info['telephone']['default_formatter'] = 'text_plain';
|
$info['telephone']['default_formatter'] = 'text_plain';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Implements hook_field_is_empty().
|
|
||||||
*/
|
|
||||||
function telephone_field_is_empty($item, $field_type) {
|
|
||||||
return !isset($item['value']) || $item['value'] === '';
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Implements hook_field_formatter_info_alter().
|
* Implements hook_field_formatter_info_alter().
|
||||||
|
|
Loading…
Reference in New Issue