You are here

ParagraphsGridFormatter.php in Paragraphs grid 8

File

src/Plugin/Field/FieldFormatter/ParagraphsGridFormatter.php
View source
<?php

namespace Drupal\paragraphs_grid\Plugin\Field\FieldFormatter;

use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityDisplayRepositoryInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Logger\LoggerChannelFactoryInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Template\Attribute;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\entity_reference_revisions\Plugin\Field\FieldFormatter\EntityReferenceRevisionsEntityFormatter;

/**
 * Plugin implementation of the 'entity reference rendered entity' formatter.
 *
 * @FieldFormatter(
 *   id = "paragraphs_grid_formatter",
 *   label = @Translation("Paragraphs Grid (rendered entity)"),
 *   description = @Translation("Display the referenced entities rendered by entity_view()."),
 *   field_types = {
 *     "entity_reference_revisions"
 *   }
 * )
 */
class ParagraphsGridFormatter extends EntityReferenceRevisionsEntityFormatter implements ContainerFactoryPluginInterface {

  /**
   * The Drupal config factory.
   *
   * @var \Drupal\Core\Config\ConfigFactory
   */
  protected $configFactory;

  /**
   * Constructs a StringFormatter 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 settings.
   * @param \Drupal\Core\Logger\LoggerChannelFactoryInterface $logger_factory
   *   The logger factory.
   * @param \Drupal\Core\Entity\EntityDisplayRepositoryInterface $entity_display_repository
   *   The entity display repository.
   * @param \Drupal\Core\Config\ConfigFactory $config_factory
   *   The entity display repository.
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, LoggerChannelFactoryInterface $logger_factory, EntityDisplayRepositoryInterface $entity_display_repository, ConfigFactory $config_factory) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings, $logger_factory, $entity_display_repository);
    $this->configFactory = $config_factory;
  }

  /**
   * {@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('logger.factory'), $container
      ->get('entity_display.repository'), $container
      ->get('config.factory'));
  }

  /**
   * {@inheritdoc}
   */
  public static function defaultSettings() {
    return [
      'container' => 'default',
    ] + parent::defaultSettings();
  }

  /**
   * {@inheritdoc}
   */
  public function settingsForm(array $form, FormStateInterface $form_state) {
    $elements = parent::settingsForm($form, $form_state);
    $module_config = $this->configFactory
      ->get('paragraphs_grid.settings');
    $grid_config = $this->configFactory
      ->get($module_config
      ->get('gridtype'));
    $elements['container'] = [
      '#type' => 'select',
      '#options' => $grid_config
        ->get('wrapper.grid.options'),
      '#title' => $this
        ->t('Select grid container'),
      '#default_value' => $this
        ->getSetting('container'),
      '#required' => $grid_config
        ->get('wrapper.grid.required'),
    ];
    return $elements;
  }

  /**
   * {@inheritdoc}
   */
  public function settingsSummary() {
    $summary = parent::settingsSummary();
    $module_config = $this->configFactory
      ->get('paragraphs_grid.settings');
    $grid_config = $this->configFactory
      ->get($module_config
      ->get('gridtype'));
    $options = $grid_config
      ->get('wrapper.grid.options');
    $container = $this
      ->getSetting('container');
    $container = isset($options[$container]) ? $options[$container] : $container;
    $summary[] = $this
      ->t('Grid container: @container', [
      '@container' => $container,
    ]);
    return $summary;
  }

  /**
   * {@inheritdoc}
   */
  public function view(FieldItemListInterface $items, $langcode = NULL) {
    $elements = parent::view($items, $langcode);
    if ($this
      ->getSetting('container') !== 'none') {
      $elements['#container_attributes'] = new Attribute([
        'class' => [
          $this
            ->getSetting('container'),
        ],
      ]);
    }
    return $elements;
  }

}

Classes

Namesort descending Description
ParagraphsGridFormatter Plugin implementation of the 'entity reference rendered entity' formatter.