function hook_entity_field_info

Define custom entity properties.

Parameters

string $entity_type: The entity type for which to define entity properties.

Return value

array An array of property information having the following optional entries:

  • definitions: An array of property definitions to add all entities of this type, keyed by property name. See Drupal\Core\TypedData\TypedDataManager::create() for a list of supported keys in property definitions.
  • optional: An array of property definitions for optional properties keyed by property name. Optional properties are properties that only exist for certain bundles of the entity type.
  • bundle map: An array keyed by bundle name containing the names of optional properties that entities of this bundle have.

See also

Drupal\Core\TypedData\TypedDataManager::create()

hook_entity_field_info_alter()

Drupal\Core\Entity\StorageControllerInterface::getPropertyDefinitions()

Related topics

1 function implements hook_entity_field_info()

Note: this list is generated by pattern matching, so it may include some functions that are not actually implementations of this hook.

field_entity_field_info in drupal/core/modules/field/field.module
Implements hook_entity_field_info() to define all configured fields.

File

drupal/core/includes/entity.api.php, line 303
Hooks provided the Entity module.

Code

function hook_entity_field_info($entity_type) {
  if (mymodule_uses_entity_type($entity_type)) {
    $info = array();
    $info['definitions']['mymodule_text'] = array(
      'type' => 'string_item',
      'list' => TRUE,
      'label' => t('The text'),
      'description' => t('A text property added by mymodule.'),
      'computed' => TRUE,
      'class' => '\\Drupal\\mymodule\\EntityComputedText',
    );
    if ($entity_type == 'node') {

      // Add a property only to entities of the 'article' bundle.
      $info['optional']['mymodule_text_more'] = array(
        'type' => 'string_item',
        'list' => TRUE,
        'label' => t('More text'),
        'computed' => TRUE,
        'class' => '\\Drupal\\mymodule\\EntityComputedMoreText',
      );
      $info['bundle map']['article'][0] = 'mymodule_text_more';
    }
    return $info;
  }
}