View source
<?php
namespace Drupal\html_formatter\Plugin\Field\FieldFormatter;
use Drupal\Component\Utility\Html;
use Drupal\Core\Field\FieldItemInterface;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\html_formatter\Plugin\HtmlFormatterTrait;
class HtmlFieldFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
use HtmlFormatterTrait;
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('path.validator'));
}
public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings) {
parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
}
public static function defaultSettings() {
return HtmlFormatterTrait::getHtmlFormatterDefaultSettings() + parent::defaultSettings();
}
public function settingsForm(array $form, FormStateInterface $form_state) {
$form = parent::settingsForm($form, $form_state);
$form += $this
->getHtmlFormatterSettingsForm();
return $form;
}
public function settingsSummary() {
$summary = parent::settingsSummary();
$summary = array_merge($summary, $this
->getHtmlFormatterSettingsSummary($this->settings));
return $summary;
}
public function viewElements(FieldItemListInterface $items, $langcode) {
$elements = [];
$url = $this
->getEntityUrl($items);
foreach ($items as $delta => $item) {
$value = $this
->getLinkedValue($this->settings, $item->value, $url);
$elements[$delta] = [
'#theme' => 'html_formatter',
'#value' => $value,
'#tag' => $this
->getSetting('tag'),
'#attributes' => [
'class' => [
$this
->getSetting('class'),
],
],
];
}
return $elements;
}
}