class MetadataGenerator

Generates in-place editing metadata for an entity field.

Hierarchy

Expanded class hierarchy of MetadataGenerator

2 files declare their use of MetadataGenerator
EditIntegrationTest.php in drupal/core/modules/editor/lib/Drupal/editor/Tests/EditIntegrationTest.php
Contains \Drupal\editor\Tests\EditorIntegrationTest.
MetadataGeneratorTest.php in drupal/core/modules/edit/lib/Drupal/edit/Tests/MetadataGeneratorTest.php
Contains \Drupal\edit\Tests\MetadataGeneratorTest.
1 string reference to 'MetadataGenerator'
edit.services.yml in drupal/core/modules/edit/edit.services.yml
drupal/core/modules/edit/edit.services.yml
1 service uses MetadataGenerator

File

drupal/core/modules/edit/lib/Drupal/edit/MetadataGenerator.php, line 19
Contains \Drupal\edit\MetadataGenerator.

Namespace

Drupal\edit
View source
class MetadataGenerator implements MetadataGeneratorInterface {

  /**
   * An object that checks if a user has access to edit a given entity field.
   *
   * @var \Drupal\edit\Access\EditEntityFieldAccessCheckInterface
   */
  protected $accessChecker;

  /**
   * An object that determines which editor to attach to a given field.
   *
   * @var \Drupal\edit\EditorSelectorInterface
   */
  protected $editorSelector;

  /**
   * The manager for editor plugins.
   *
   * @var \Drupal\Component\Plugin\PluginManagerInterface
   */
  protected $editorManager;

  /**
   * Constructs a new MetadataGenerator.
   *
   * @param \Drupal\edit\Access\EditEntityFieldAccessCheckInterface $access_checker
   *   An object that checks if a user has access to edit a given field.
   * @param \Drupal\edit\EditorSelectorInterface $editor_selector
   *   An object that determines which editor to attach to a given field.
   * @param \Drupal\Component\Plugin\PluginManagerInterface
   *   The manager for editor plugins.
   */
  public function __construct(EditEntityFieldAccessCheckInterface $access_checker, EditorSelectorInterface $editor_selector, PluginManagerInterface $editor_manager) {
    $this->accessChecker = $access_checker;
    $this->editorSelector = $editor_selector;
    $this->editorManager = $editor_manager;
  }

  /**
   * Implements \Drupal\edit\MetadataGeneratorInterface::generate().
   */
  public function generate(EntityInterface $entity, FieldInstance $instance, $langcode, $view_mode) {
    $field_name = $instance['field_name'];

    // Early-return if user does not have access.
    $access = $this->accessChecker
      ->accessEditEntityField($entity, $field_name);
    if (!$access) {
      return array(
        'access' => FALSE,
      );
    }

    // Early-return if no editor is available.
    $formatter_id = entity_get_render_display($entity, $view_mode)
      ->getFormatter($instance['field_name'])
      ->getPluginId();
    $items = $entity
      ->getTranslation($langcode, FALSE)
      ->get($field_name)
      ->getValue();
    $editor_id = $this->editorSelector
      ->getEditor($formatter_id, $instance, $items);
    if (!isset($editor_id)) {
      return array(
        'access' => FALSE,
      );
    }

    // Gather metadata, allow the editor to add additional metadata of its own.
    $label = $instance['label'];
    $editor = $this->editorManager
      ->createInstance($editor_id);
    $metadata = array(
      'label' => check_plain($label),
      'access' => TRUE,
      'editor' => $editor_id,
      'aria' => t('Entity @type @id, field @field', array(
        '@type' => $entity
          ->entityType(),
        '@id' => $entity
          ->id(),
        '@field' => $label,
      )),
    );
    $custom_metadata = $editor
      ->getMetadata($instance, $items);
    if (count($custom_metadata)) {
      $metadata['custom'] = $custom_metadata;
    }
    return $metadata;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
MetadataGenerator::$accessChecker protected property An object that checks if a user has access to edit a given entity field.
MetadataGenerator::$editorManager protected property The manager for editor plugins.
MetadataGenerator::$editorSelector protected property An object that determines which editor to attach to a given field.
MetadataGenerator::generate public function Implements \Drupal\edit\MetadataGeneratorInterface::generate(). Overrides MetadataGeneratorInterface::generate
MetadataGenerator::__construct public function Constructs a new MetadataGenerator.