You are here

public function MetadataGenerator::generateFieldMetadata in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/quickedit/src/MetadataGenerator.php \Drupal\quickedit\MetadataGenerator::generateFieldMetadata()

Generates in-place editing metadata for an entity field.

Parameters

\Drupal\Core\Field\FieldItemListInterface $items: The field values to be in-place edited.

string $view_mode: The view mode the field should be rerendered in.

Return value

array An array containing metadata with the following keys:

  • label: the user-visible label for the field.
  • access: whether the current user may edit the field or not.
  • editor: which editor should be used for the field.
  • aria: the ARIA label.
  • custom: (optional) any additional metadata that the editor provides.

Overrides MetadataGeneratorInterface::generateFieldMetadata

File

core/modules/quickedit/src/MetadataGenerator.php, line 65

Class

MetadataGenerator
Generates in-place editing metadata for an entity field.

Namespace

Drupal\quickedit

Code

public function generateFieldMetadata(FieldItemListInterface $items, $view_mode) {
  $entity = $items
    ->getEntity();
  $field_name = $items
    ->getFieldDefinition()
    ->getName();

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

  // Early-return if no editor is available.
  $formatter_id = EntityViewDisplay::collectRenderDisplay($entity, $view_mode)
    ->getRenderer($field_name)
    ->getPluginId();
  $editor_id = $this->editorSelector
    ->getEditor($formatter_id, $items);
  if (!isset($editor_id)) {
    return [
      'access' => FALSE,
    ];
  }

  // Gather metadata, allow the editor to add additional metadata of its own.
  $label = $items
    ->getFieldDefinition()
    ->getLabel();
  $editor = $this->editorManager
    ->createInstance($editor_id);
  $metadata = [
    'label' => $label,
    'access' => TRUE,
    'editor' => $editor_id,
  ];
  $custom_metadata = $editor
    ->getMetadata($items);
  if (count($custom_metadata)) {
    $metadata['custom'] = $custom_metadata;
  }
  return $metadata;
}