ContentModerationStateFormatter.php

Same filename in other branches
  1. 9 core/modules/content_moderation/src/Plugin/Field/FieldFormatter/ContentModerationStateFormatter.php
  2. 8.9.x core/modules/content_moderation/src/Plugin/Field/FieldFormatter/ContentModerationStateFormatter.php
  3. 10 core/modules/content_moderation/src/Plugin/Field/FieldFormatter/ContentModerationStateFormatter.php

Namespace

Drupal\content_moderation\Plugin\Field\FieldFormatter

File

core/modules/content_moderation/src/Plugin/Field/FieldFormatter/ContentModerationStateFormatter.php

View source
<?php

namespace Drupal\content_moderation\Plugin\Field\FieldFormatter;

use Drupal\content_moderation\ModerationInformationInterface;
use Drupal\Core\Field\Attribute\FieldFormatter;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Field\FormatterBase;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Plugin implementation of the 'content_moderation_state' formatter.
 */
class ContentModerationStateFormatter extends FormatterBase {
    
    /**
     * The moderation information service.
     *
     * @var \Drupal\content_moderation\ModerationInformationInterface
     */
    protected $moderationInformation;
    
    /**
     * Create an instance of ContentModerationStateFormatter.
     */
    public function __construct($plugin_id, $plugin_definition, FieldDefinitionInterface $field_definition, array $settings, $label, $view_mode, array $third_party_settings, ModerationInformationInterface $moderation_information) {
        parent::__construct($plugin_id, $plugin_definition, $field_definition, $settings, $label, $view_mode, $third_party_settings);
        $this->moderationInformation = $moderation_information;
    }
    
    /**
     * {@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('content_moderation.moderation_information'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function viewElements(FieldItemListInterface $items, $langcode) {
        $elements = [];
        $workflow = $this->moderationInformation
            ->getWorkflowForEntity($items->getEntity());
        foreach ($items as $delta => $item) {
            $elements[$delta] = [
                '#markup' => $workflow->getTypePlugin()
                    ->getState($item->value)
                    ->label(),
            ];
        }
        return $elements;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function isApplicable(FieldDefinitionInterface $field_definition) {
        return $field_definition->getName() === 'moderation_state' && $field_definition->getTargetEntityTypeId() !== 'content_moderation_state';
    }

}

Classes

Title Deprecated Summary
ContentModerationStateFormatter Plugin implementation of the 'content_moderation_state' formatter.

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.