You are here

class EntityDefaultMetadataController in Entity API 7

Default controller for generating some basic metadata for CRUD entity types.

Hierarchy

Expanded class hierarchy of EntityDefaultMetadataController

1 string reference to 'EntityDefaultMetadataController'
entity_entity_property_info in ./entity.info.inc
Implements hook_entity_property_info().

File

./entity.info.inc, line 59
Provides basic entity property info for entities provided via the CRUD API, as well as property info for all entity types defined by core. For that the respective modules/MODULE.info.inc files are included.

View source
class EntityDefaultMetadataController {
  protected $type, $info;
  public function __construct($type) {
    $this->type = $type;
    $this->info = entity_get_info($type);
  }
  public function entityPropertyInfo() {
    $entity_label = drupal_strtolower($this->info['label']);

    // Provide defaults based on the schema.
    $info['properties'] = $this
      ->convertSchema();
    foreach ($info['properties'] as $name => &$property) {

      // Add a description.
      $property['description'] = t('@entity "@property" property.', array(
        '@entity' => drupal_ucfirst($entity_label),
        '@property' => $name,
      ));
    }

    // Set better metadata for known entity keys.
    $id_key = $this->info['entity keys']['id'];
    if (!empty($this->info['entity keys']['name']) && ($key = $this->info['entity keys']['name'])) {
      $info['properties'][$key]['type'] = 'token';
      $info['properties'][$key]['label'] = t('Machine-readable name');
      $info['properties'][$key]['description'] = t('The machine-readable name identifying this @entity.', array(
        '@entity' => $entity_label,
      ));
      $info['properties'][$id_key]['label'] = t('Internal, numeric @entity ID', array(
        '@entity' => $entity_label,
      ));
      $info['properties'][$id_key]['description'] = t('The ID used to identify this @entity internally.', array(
        '@entity' => $entity_label,
      ));
    }
    else {
      $info['properties'][$id_key]['label'] = t('@entity ID', array(
        '@entity' => drupal_ucfirst($entity_label),
      ));
      $info['properties'][$id_key]['description'] = t('The unique ID of the @entity.', array(
        '@entity' => $entity_label,
      ));
    }

    // Care for the bundle.
    if (!empty($this->info['entity keys']['bundle']) && ($key = $this->info['entity keys']['bundle'])) {
      $info['properties'][$key]['type'] = 'token';
      $info['properties'][$key]['options list'] = array(
        get_class($this),
        'bundleOptionsList',
      );
    }

    // Care for the label.
    if (!empty($this->info['entity keys']['label']) && ($key = $this->info['entity keys']['label'])) {
      $info['properties'][$key]['label'] = t('Label');
      $info['properties'][$key]['description'] = t('The human readable label.');
    }

    // Add a computed property for the entity URL and expose it to views.
    if (empty($info['properties']['url']) && !empty($this->info['uri callback'])) {
      $info['properties']['url'] = array(
        'label' => t('URL'),
        'description' => t('The URL of the entity.'),
        'getter callback' => 'entity_metadata_entity_get_properties',
        'type' => 'uri',
        'computed' => TRUE,
        'entity views field' => TRUE,
      );
    }
    return array(
      $this->type => $info,
    );
  }

  /**
   * A options list callback returning all bundles for an entity type.
   */
  public static function bundleOptionsList($name, $info) {
    if (!empty($info['parent']) && ($type = $info['parent'])) {
      $entity_info = $info['parent']
        ->entityInfo();
      $options = array();
      foreach ($entity_info['bundles'] as $name => $bundle_info) {
        $options[$name] = $bundle_info['label'];
      }
      return $options;
    }
  }

  /**
   * Return a set of properties for an entity based on the schema definition
   */
  protected function convertSchema() {
    return entity_metadata_convert_schema($this->info['base table']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
EntityDefaultMetadataController::$type protected property
EntityDefaultMetadataController::bundleOptionsList public static function A options list callback returning all bundles for an entity type.
EntityDefaultMetadataController::convertSchema protected function Return a set of properties for an entity based on the schema definition
EntityDefaultMetadataController::entityPropertyInfo public function
EntityDefaultMetadataController::__construct public function