View source
<?php
namespace Drupal\layout_builder\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Cache\CacheableMetadata;
use Drupal\Core\Entity\EntityFieldManagerInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Plugin\ContextAwarePluginInterface;
use Drupal\Core\Render\Element;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;
class ExtraFieldBlock extends BlockBase implements ContextAwarePluginInterface, ContainerFactoryPluginInterface {
protected $entityFieldManager;
protected $fieldName;
protected $entityTypeManager;
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityFieldManagerInterface $entity_field_manager) {
$this->entityTypeManager = $entity_type_manager;
$this->entityFieldManager = $entity_field_manager;
list(, , , $field_name) = explode(static::DERIVATIVE_SEPARATOR, $plugin_id, 4);
assert(!empty($field_name));
$this->fieldName = $field_name;
parent::__construct($configuration, $plugin_id, $plugin_definition);
}
public function defaultConfiguration() {
return [
'label_display' => FALSE,
];
}
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static($configuration, $plugin_id, $plugin_definition, $container
->get('entity_type.manager'), $container
->get('entity_field.manager'));
}
protected function getEntity() {
return $this
->getContextValue('entity');
}
public function build() {
$entity = $this
->getEntity();
$extra_fields = $this->entityFieldManager
->getExtraFields($entity
->getEntityTypeId(), $entity
->bundle());
if (!isset($extra_fields['display'][$this->fieldName])) {
$build = [];
}
else {
$build = [
'#extra_field_placeholder_field_name' => $this->fieldName,
'#markup' => $this
->t('Placeholder for the @preview_fallback', [
'@preview_fallback' => $this
->getPreviewFallbackString(),
]),
];
}
CacheableMetadata::createFromObject($this)
->applyTo($build);
return $build;
}
public function getPreviewFallbackString() {
$entity = $this
->getEntity();
$extra_fields = $this->entityFieldManager
->getExtraFields($entity
->getEntityTypeId(), $entity
->bundle());
return new TranslatableMarkup('"@field" field', [
'@field' => $extra_fields['display'][$this->fieldName]['label'],
]);
}
public static function replaceFieldPlaceholder(array &$build, array $built_field, $field_name) {
foreach (Element::children($build) as $child) {
if (isset($build[$child]['#extra_field_placeholder_field_name']) && $build[$child]['#extra_field_placeholder_field_name'] === $field_name) {
$placeholder_cache = CacheableMetadata::createFromRenderArray($build[$child]);
$built_cache = CacheableMetadata::createFromRenderArray($built_field);
$merged_cache = $placeholder_cache
->merge($built_cache);
$build[$child] = $built_field;
$merged_cache
->applyTo($build);
}
else {
static::replaceFieldPlaceholder($build[$child], $built_field, $field_name);
}
}
}
protected function blockAccess(AccountInterface $account) {
return $this
->getEntity()
->access('view', $account, TRUE);
}
}