MetadataGenerator.php in Drupal 8
File
core/modules/quickedit/src/MetadataGenerator.php
View source
<?php
namespace Drupal\quickedit;
use Drupal\Component\Plugin\PluginManagerInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\quickedit\Access\QuickEditEntityFieldAccessCheckInterface;
use Drupal\Core\Entity\Entity\EntityViewDisplay;
class MetadataGenerator implements MetadataGeneratorInterface {
protected $accessChecker;
protected $editorSelector;
protected $editorManager;
public function __construct(QuickEditEntityFieldAccessCheckInterface $access_checker, EditorSelectorInterface $editor_selector, PluginManagerInterface $editor_manager) {
$this->accessChecker = $access_checker;
$this->editorSelector = $editor_selector;
$this->editorManager = $editor_manager;
}
public function generateEntityMetadata(EntityInterface $entity) {
return [
'label' => $entity
->label(),
];
}
public function generateFieldMetadata(FieldItemListInterface $items, $view_mode) {
$entity = $items
->getEntity();
$field_name = $items
->getFieldDefinition()
->getName();
$access = $this->accessChecker
->accessEditEntityField($entity, $field_name);
if (!$access) {
return [
'access' => FALSE,
];
}
$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,
];
}
$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;
}
}