whitelabel-8.x-2.x-dev/src/Plugin/Field/FieldFormatter/WhiteLabelPreviewFormatter.php
src/Plugin/Field/FieldFormatter/WhiteLabelPreviewFormatter.php
<?php
namespace Drupal\whitelabel\Plugin\Field\FieldFormatter;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldFormatter\EntityReferenceFormatterBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Plugin implementation of the 'White label preview' formatter.
*
* @FieldFormatter(
* id = "white_label_preview",
* label = @Translation("White label preview"),
* description = @Translation("Display the referenced entities rendered by
* entity_view()."), field_types = {
* "entity_reference",
* "entity_reference_revisions"
* }
* )
*/
class WhiteLabelPreviewFormatter extends EntityReferenceFormatterBase {
/**
* The entity type manager.
*
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
*/
protected $entityTypeManager;
/**
* Constructs an EntityReferenceEntityFormatter instance.
*
* @param string $plugin_id
* The plugin_id for the formatter.
* @param mixed $plugin_definition
* The plugin implementation definition.
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The definition of the field to which the formatter is associated.
* @param array $settings
* The formatter settings.
* @param string $label
* The formatter label display setting.
* @param string $view_mode
* The view mode.
* @param array $third_party_settings
* Any third party settings.
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
* The entity type manager.
*/
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EntityTypeManagerInterface $entity_type_manager) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
$this->entityTypeManager = $entity_type_manager;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$plugin_id,
$plugin_definition,
$configuration['field_definition'],
$configuration['settings'],
$configuration['label'],
$configuration['view_mode'],
$configuration['third_party_settings'],
$container->get('entity_type.manager')
);
}
/**
* {@inheritdoc}
*/
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
/** @var \Drupal\whitelabel\Entity\WhiteLabelInterface $entity */
foreach ($this->getEntitiesToView($items, $langcode) as $delta => $entity) {
$preview['container'] = [
'#type' => 'details',
'#title' => t('White label preview'),
'#open' => TRUE,
'#attributes' => [
'class' => ['whitelabel_preview'],
],
];
$preview['container']['preview'] = [
'#theme' => 'whitelabel_preview',
'#whitelabel' => $entity,
];
$elements[$delta] = $preview;
}
return $elements;
}
/**
* {@inheritdoc}
*/
public static function isApplicable(FieldDefinitionInterface $field_definition) {
// This formatter is only available for White label reference fields.
$target_type = $field_definition->getFieldStorageDefinition()->getSetting('target_type');
return ($target_type == 'whitelabel');
}
}
