diff --git a/src/Plugin/Block/EntityField.php b/src/Plugin/Block/EntityField.php index 388fe4a..390f9e0 100644 --- a/src/Plugin/Block/EntityField.php +++ b/src/Plugin/Block/EntityField.php @@ -10,14 +10,15 @@ namespace Drupal\ctools\Plugin\Block; use Drupal\Component\Utility\NestedArray; use Drupal\Core\Block\BlockBase; use Drupal\Core\Cache\CacheableMetadata; -use Drupal\Core\Entity\EntityManagerInterface; +use Drupal\Core\Entity\EntityFieldManagerInterface; +use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\Entity\FieldableEntityInterface; -use Drupal\Core\Field\FieldItemInterface; use Drupal\Core\Field\FieldTypePluginManagerInterface; use Drupal\Core\Field\FormatterPluginManager; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Plugin\ContainerFactoryPluginInterface; use Drupal\Core\Plugin\ContextAwarePluginInterface; +use Drupal\Core\Session\AccountInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -31,11 +32,18 @@ use Symfony\Component\DependencyInjection\ContainerInterface; class EntityField extends BlockBase implements ContextAwarePluginInterface, ContainerFactoryPluginInterface { /** - * The entity manager. + * The entity type manager. * - * @var \Drupal\Core\Entity\EntityManagerInterface + * @var \Drupal\Core\Entity\EntityTypeManagerInterface */ - protected $entityManager; + protected $entityTypeManager; + + /** + * The entity field manager. + * + * @var \Drupal\Core\Entity\EntityFieldManagerInterface + */ + protected $entityFieldManager; /** * The field type manager. @@ -88,11 +96,16 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont * The plugin ID for the plugin instance. * @param mixed $plugin_definition * The plugin implementation definition. - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. + * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager + * The entity type manager. + * @param \Drupal\Core\Entity\EntityFieldManagerInterface $entity_field_manager + * The entity field manager. + * @param \Drupal\Core\Field\FormatterPluginManager $formatter_manager + * The formatter manager. */ - public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityManagerInterface $entity_manager, FieldTypePluginManagerInterface $field_type_manager, FormatterPluginManager $formatter_manager) { - $this->entityManager = $entity_manager; + public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager, FieldTypePluginManagerInterface $field_type_manager, FormatterPluginManager $formatter_manager) { + $this->entityTypeManager = $entity_type_manager; + $this->entityFieldManager = $entity_field_manager; $this->fieldTypeManager = $field_type_manager; $this->formatterManager = $formatter_manager; @@ -112,7 +125,8 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont $configuration, $plugin_id, $plugin_definition, - $container->get('entity.manager'), + $container->get('entity_type.manager'), + $container->get('entity_field.manager'), $container->get('plugin.manager.field.field_type'), $container->get('plugin.manager.field.formatter') ); @@ -125,17 +139,31 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont /** @var \Drupal\Core\Entity\FieldableEntityInterface $entity */ $entity = $this->getContextValue('entity'); + $build = []; + if ($entity instanceof FieldableEntityInterface && $entity->hasField($this->fieldName)) { - /** @var \Drupal\Core\Field\FieldItemInterface $field */ + /** @var \Drupal\Core\Field\FieldItemListInterface $field */ $field = $entity->{$this->fieldName}; - $display_settings = $this->getConfiguration()['formatter']; - $build['field'] = $field->view($display_settings); - - CacheableMetadata::createFromObject($entity) - ->applyTo($build); - return $build; + $access_handler = $this->entityTypeManager->getAccessControlHandler($this->entityTypeId); + if ($access_handler->access($entity, 'view') && $access_handler->fieldAccess('view', $this->getFieldDefinition(), NULL, $field)) { + $display_settings = $this->getConfiguration()['formatter']; + $build['field'] = $field->view($display_settings); + } } + + CacheableMetadata::createFromObject($entity) + ->applyTo($build); + + return $build; + } + + /** + * @inheritDoc + */ + protected function blockAccess(AccountInterface $account) { + $access_handler = $this->entityTypeManager->getAccessControlHandler($this->entityTypeId); + return $access_handler->fieldAccess('view', $this->getFieldDefinition(), $account, NULL, TRUE); } /** @@ -249,13 +277,13 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont /** * Gets the field definition. * - * @return \Drupal\Core\Field\FieldStorageDefinitionInterface + * @return \Drupal\Core\Field\FieldDefinitionInterface */ protected function getFieldDefinition() { if (empty($this->fieldDefinition)) { - $field_map = $this->entityManager->getFieldMap(); + $field_map = $this->entityFieldManager->getFieldMap(); $bundle = reset($field_map[$this->entityTypeId][$this->fieldName]['bundles']); - $field_definitions = $this->entityManager->getFieldDefinitions($this->entityTypeId, $bundle); + $field_definitions = $this->entityFieldManager->getFieldDefinitions($this->entityTypeId, $bundle); $this->fieldDefinition = $field_definitions[$this->fieldName]; } return $this->fieldDefinition; @@ -268,7 +296,7 @@ class EntityField extends BlockBase implements ContextAwarePluginInterface, Cont */ protected function getFieldStorageDefinition() { if (empty($this->fieldStorageDefinition)) { - $field_definitions = $this->entityManager->getFieldStorageDefinitions($this->entityTypeId); + $field_definitions = $this->entityFieldManager->getFieldStorageDefinitions($this->entityTypeId); $this->fieldStorageDefinition = $field_definitions[$this->fieldName]; } return $this->fieldStorageDefinition;