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;
class EditorNoteTableFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
protected $editorNoteHelper;
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;
}
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'));
}
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;
}
}