Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace Drupal\customrcpf\Plugin\Field\FieldFormatter;
- use Drupal\Component\Utility\Unicode;
- use Drupal\Core\Field\FieldDefinitionInterface;
- use Drupal\Core\Field\FieldItemListInterface;
- use Drupal\Core\Field\FormatterBase;
- use Drupal\Core\Form\FormStateInterface;
- use Drupal\Core\Path\PathValidatorInterface;
- use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
- use Drupal\Core\Url;
- use Drupal\link\LinkItemInterface;
- use Symfony\Component\DependencyInjection\ContainerInterface;
- /**
- * Plugin implementation of the 'rcpf_link' formatter.
- *
- * @FieldFormatter(
- * id = "rcpf_link",
- * label = @Translation("RCPF link"),
- * field_types = {
- * "link"
- * }
- * )
- */
- class RCPFLinkFormatter extends FormatterBase implements ContainerFactoryPluginInterface {
- /**
- * The path validator service.
- *
- * @var \Drupal\Core\Path\PathValidatorInterface
- */
- protected $pathValidator;
- /**
- * {@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('path.validator')
- );
- }
- /**
- * Constructs a new LinkFormatter.
- *
- * @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
- * Third party settings.
- * @param \Drupal\Core\Path\PathValidatorInterface $path_validator
- * The path validator service.
- */
- public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, PathValidatorInterface $path_validator) {
- parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
- $this->pathValidator = $path_validator;
- }
- /**
- * {@inheritdoc}
- */
- public static function defaultSettings() {
- return [
- 'trim_length' => '80',
- 'url_only' => '',
- 'url_plain' => '',
- 'rel' => '',
- 'target' => '',
- ] + parent::defaultSettings();
- }
- /**
- * {@inheritdoc}
- */
- public function viewElements(FieldItemListInterface $items, $langcode) {
- $element = [];
- $entity = $items->getEntity();
- foreach ($items as $delta => $item) {
- $url = $item->getUrl() ?: Url::fromRoute('<none>');
- $title = \Drupal::token()->replace($item->title, [$entity->getEntityTypeId() => $entity], ['clear' => TRUE]);
- $element[$delta] = [
- '#theme' => 'customrcpf_formatter_link',
- '#title' => $title,
- '#url' => $url,
- ];
- if (!empty($item->_attributes)) {
- $element[$delta]['#options'] += ['attributes' => []];
- $element[$delta]['#options']['attributes'] += $item->_attributes;
- // Unset field item attributes since they have been included in the
- // formatter output and should not be rendered in the field template.
- unset($item->_attributes);
- }
- }
- return $element;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement