You are here

EditorNoteTableFormatter.php in Editor Notes 8

File

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

namespace Drupal\editor_note\Plugin\Field\FieldFormatter;

use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\editor_note\EditorNoteHelperService;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Plugin implementation of the 'Editor Note' formatter.
 *
 * @FieldFormatter(
 *   id = "editor_note_table_formatter",
 *   label = @Translation("Editor Note table"),
 *   module = "editor_note",
 *   field_types = {
 *     "editor_note_item"
 *   }
 * )
 */
class EditorNoteTableFormatter extends FormatterBase implements ContainerFactoryPluginInterface {

  /**
   * The editor note helper.
   *
   * @var \Drupal\editor_note\EditorNoteHelperService
   */
  protected $editorNoteHelper;

  /**
   * Constructs a CountryDefaultWidget object.
   *
   * @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\editor_note\EditorNoteHelperService $editorNoteHelper
   *   Editor note helpers.
   */
  public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, EditorNoteHelperService $editorNoteHelper) {
    parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
    $this->editorNoteHelper = $editorNoteHelper;
  }

  /**
   * {@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('editor_note.helper'));
  }

  /**
   * {@inheritdoc}
   */
  public function viewElements(FieldItemListInterface $items, $langcode) {
    $entity = $items
      ->getEntity();
    $notes = $this->editorNoteHelper
      ->getNotesByEntityAndField($entity
      ->id(), $this->fieldDefinition
      ->getName());
    $field = $this->fieldDefinition;
    $table = $this->editorNoteHelper
      ->generateTable($field, $notes, FALSE);
    if (!isset($table['notes_table'])) {
      return [];
    }
    $data = $table['notes_table'];
    $data['#prefix'] = $table['#prefix'];
    $data['#suffix'] = $table['#suffix'];
    return $data;
  }

}

Classes

Namesort descending Description
EditorNoteTableFormatter Plugin implementation of the 'Editor Note' formatter.