You are here

class EntityDefaultExtraFieldsController in Entity API 7

Default controller for generating extra fields based on property metadata.

By default a display extra field for each property not being a field, ID or bundle is generated.

Hierarchy

Expanded class hierarchy of EntityDefaultExtraFieldsController

File

./entity.info.inc, line 211
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 EntityDefaultExtraFieldsController implements EntityExtraFieldsControllerInterface {

  /**
   * @var string
   */
  protected $entityType;

  /**
   * @var array
   */
  protected $entityInfo;

  /**
   * Constructor.
   */
  public function __construct($type) {
    $this->entityType = $type;
    $this->entityInfo = entity_get_info($type);
    $this->propertyInfo = entity_get_property_info($type);
  }

  /**
   * Implements EntityExtraFieldsControllerInterface::fieldExtraFields().
   */
  public function fieldExtraFields() {
    $extra = array();
    foreach ($this->propertyInfo['properties'] as $name => $property_info) {

      // Skip adding the ID or bundle.
      if ($this->entityInfo['entity keys']['id'] == $name || $this->entityInfo['entity keys']['bundle'] == $name) {
        continue;
      }
      $extra[$this->entityType][$this->entityType]['display'][$name] = $this
        ->generateExtraFieldInfo($name, $property_info);
    }

    // Handle bundle properties.
    $this->propertyInfo += array(
      'bundles' => array(),
    );
    foreach ($this->propertyInfo['bundles'] as $bundle_name => $info) {
      foreach ($info['properties'] as $name => $property_info) {
        if (empty($property_info['field'])) {
          $extra[$this->entityType][$bundle_name]['display'][$name] = $this
            ->generateExtraFieldInfo($name, $property_info);
        }
      }
    }
    return $extra;
  }

  /**
   * Generates the display field info for a given property.
   */
  protected function generateExtraFieldInfo($name, $property_info) {
    $info = array(
      'label' => $property_info['label'],
      'weight' => 0,
    );
    if (!empty($property_info['description'])) {
      $info['description'] = $property_info['description'];
    }
    return $info;
  }

}

Members