You are here

public static function ColorItem::propertyDefinitions in Color API 8

Defines field item properties.

Properties that are required to constitute a valid, non-empty item should be denoted with \Drupal\Core\TypedData\DataDefinition::setRequired().

Return value

\Drupal\Core\TypedData\DataDefinitionInterface[] An array of property definitions of contained properties, keyed by property name.

Overrides FieldItemInterface::propertyDefinitions

See also

\Drupal\Core\Field\BaseFieldDefinition

File

src/Plugin/Field/FieldType/ColorItem.php, line 52

Class

ColorItem
Provides the Color field.

Namespace

Drupal\colorapi\Plugin\Field\FieldType

Code

public static function propertyDefinitions(FieldStorageDefinitionInterface $field_definition) {

  // Retrieve the Typed Data Plugin Manager. This will be used to retrieve
  // the data definitions for the properties of this Field type.
  $typed_data_manager = \Drupal::typedDataManager();

  // The Plugin ID of the Typed Data data type the name property will store:
  $string_data_type = 'string';

  // Retrieve the data definition for String Simple Data types.
  $string_definition_info = $typed_data_manager
    ->getDefinition($string_data_type);

  // Use the definition class for the data type to create a new String object
  // and set some values on it.
  $properties['name'] = $string_definition_info['definition_class']::create($string_data_type)
    ->setLabel(t('Name'))
    ->setDescription(t('The human readable name of the color'));

  // The Plugin ID of the Typed Data data type the color property will store:
  $color_data_type = 'colorapi_color';

  // Retrieve the data definition for Color complex Data types.
  $color_definition_info = $typed_data_manager
    ->getDefinition($color_data_type);

  // Use the definition class for the data type to create a new Color object
  // and set some values on it.
  $properties['color'] = $color_definition_info['definition_class']::create($color_data_type)
    ->setLabel(t('Color'))
    ->setDescription(t('The color, in hexadecimal and RGB format.'))
    ->setRequired(TRUE);
  return $properties;
}