class ContentModeration

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration
  2. 10 core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration
  3. 11.x core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php \Drupal\content_moderation\Plugin\WorkflowType\ContentModeration

Attaches workflows to content entity types and their bundles.

Plugin annotation


@WorkflowType(
  id = "content_moderation",
  label = @Translation("Content moderation"),
  required_states = {
    "draft",
    "published",
  },
  forms = {
    "configure" = "\Drupal\content_moderation\Form\ContentModerationConfigureForm",
    "state" = "\Drupal\content_moderation\Form\ContentModerationStateForm"
  },
)

Hierarchy

Expanded class hierarchy of ContentModeration

File

core/modules/content_moderation/src/Plugin/WorkflowType/ContentModeration.php, line 34

Namespace

Drupal\content_moderation\Plugin\WorkflowType
View source
class ContentModeration extends WorkflowTypeBase implements ContentModerationInterface, ContainerFactoryPluginInterface {
    use StringTranslationTrait;
    
    /**
     * The entity type manager.
     *
     * @var \Drupal\Core\Entity\EntityTypeManagerInterface
     */
    protected $entityTypeManager;
    
    /**
     * The entity type bundle info service.
     *
     * @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
     */
    protected $entityTypeBundleInfo;
    
    /**
     * The moderation information service.
     *
     * @var \Drupal\content_moderation\ModerationInformationInterface
     */
    protected $moderationInfo;
    
    /**
     * Constructs a ContentModeration object.
     *
     * @param array $configuration
     *   A configuration array containing information about the plugin instance.
     * @param string $plugin_id
     *   The plugin_id for the plugin instance.
     * @param mixed $plugin_definition
     *   The plugin implementation definition.
     * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
     *   The entity type manager.
     * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_info
     *   Moderation information service.
     */
    public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, EntityTypeBundleInfoInterface $entity_type_bundle_info, ModerationInformationInterface $moderation_info) {
        parent::__construct($configuration, $plugin_id, $plugin_definition);
        $this->entityTypeManager = $entity_type_manager;
        $this->entityTypeBundleInfo = $entity_type_bundle_info;
        $this->moderationInfo = $moderation_info;
    }
    
    /**
     * {@inheritdoc}
     */
    public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
        return new static($configuration, $plugin_id, $plugin_definition, $container->get('entity_type.manager'), $container->get('entity_type.bundle.info'), $container->get('content_moderation.moderation_information'));
    }
    
    /**
     * {@inheritdoc}
     */
    public function getState($state_id) {
        $state = parent::getState($state_id);
        if (isset($this->configuration['states'][$state->id()]['published']) && isset($this->configuration['states'][$state->id()]['default_revision'])) {
            $state = new ContentModerationState($state, $this->configuration['states'][$state->id()]['published'], $this->configuration['states'][$state->id()]['default_revision']);
        }
        else {
            $state = new ContentModerationState($state);
        }
        return $state;
    }
    
    /**
     * {@inheritdoc}
     */
    public function workflowHasData(WorkflowInterface $workflow) {
        return (bool) $this->entityTypeManager
            ->getStorage('content_moderation_state')
            ->getQuery()
            ->condition('workflow', $workflow->id())
            ->count()
            ->accessCheck(FALSE)
            ->range(0, 1)
            ->execute();
    }
    
    /**
     * {@inheritdoc}
     */
    public function workflowStateHasData(WorkflowInterface $workflow, StateInterface $state) {
        return (bool) $this->entityTypeManager
            ->getStorage('content_moderation_state')
            ->getQuery()
            ->condition('workflow', $workflow->id())
            ->condition('moderation_state', $state->id())
            ->count()
            ->accessCheck(FALSE)
            ->range(0, 1)
            ->execute();
    }
    
    /**
     * {@inheritdoc}
     */
    public function getEntityTypes() {
        return array_keys($this->configuration['entity_types']);
    }
    
    /**
     * {@inheritdoc}
     */
    public function getBundlesForEntityType($entity_type_id) {
        return isset($this->configuration['entity_types'][$entity_type_id]) ? $this->configuration['entity_types'][$entity_type_id] : [];
    }
    
    /**
     * {@inheritdoc}
     */
    public function appliesToEntityTypeAndBundle($entity_type_id, $bundle_id) {
        return in_array($bundle_id, $this->getBundlesForEntityType($entity_type_id), TRUE);
    }
    
    /**
     * {@inheritdoc}
     */
    public function removeEntityTypeAndBundle($entity_type_id, $bundle_id) {
        if (!isset($this->configuration['entity_types'][$entity_type_id])) {
            return;
        }
        $key = array_search($bundle_id, $this->configuration['entity_types'][$entity_type_id], TRUE);
        if ($key !== FALSE) {
            unset($this->configuration['entity_types'][$entity_type_id][$key]);
            if (empty($this->configuration['entity_types'][$entity_type_id])) {
                unset($this->configuration['entity_types'][$entity_type_id]);
            }
            else {
                $this->configuration['entity_types'][$entity_type_id] = array_values($this->configuration['entity_types'][$entity_type_id]);
            }
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function addEntityTypeAndBundle($entity_type_id, $bundle_id) {
        if (!$this->appliesToEntityTypeAndBundle($entity_type_id, $bundle_id)) {
            $this->configuration['entity_types'][$entity_type_id][] = $bundle_id;
            sort($this->configuration['entity_types'][$entity_type_id]);
            ksort($this->configuration['entity_types']);
        }
    }
    
    /**
     * {@inheritdoc}
     */
    public function defaultConfiguration() {
        return [
            'states' => [
                'draft' => [
                    'label' => 'Draft',
                    'published' => FALSE,
                    'default_revision' => FALSE,
                    'weight' => 0,
                ],
                'published' => [
                    'label' => 'Published',
                    'published' => TRUE,
                    'default_revision' => TRUE,
                    'weight' => 1,
                ],
            ],
            'transitions' => [
                'create_new_draft' => [
                    'label' => 'Create New Draft',
                    'to' => 'draft',
                    'weight' => 0,
                    'from' => [
                        'draft',
                        'published',
                    ],
                ],
                'publish' => [
                    'label' => 'Publish',
                    'to' => 'published',
                    'weight' => 1,
                    'from' => [
                        'draft',
                        'published',
                    ],
                ],
            ],
            'entity_types' => [],
        ];
    }
    
    /**
     * {@inheritdoc}
     */
    public function calculateDependencies() {
        $dependencies = parent::calculateDependencies();
        foreach ($this->getEntityTypes() as $entity_type_id) {
            $entity_definition = $this->entityTypeManager
                ->getDefinition($entity_type_id);
            foreach ($this->getBundlesForEntityType($entity_type_id) as $bundle) {
                $dependency = $entity_definition->getBundleConfigDependency($bundle);
                $dependencies[$dependency['type']][] = $dependency['name'];
            }
        }
        return $dependencies;
    }
    
    /**
     * {@inheritdoc}
     */
    public function onDependencyRemoval(array $dependencies) {
        $changed = parent::onDependencyRemoval($dependencies);
        // When bundle config entities are removed, ensure they are cleaned up from
        // the workflow.
        foreach ($dependencies['config'] as $removed_config) {
            if ($entity_type_id = $removed_config->getEntityType()
                ->getBundleOf()) {
                $bundle_id = $removed_config->id();
                $this->removeEntityTypeAndBundle($entity_type_id, $bundle_id);
                $changed = TRUE;
            }
        }
        // When modules that provide entity types are removed, ensure they are also
        // removed from the workflow.
        if (!empty($dependencies['module'])) {
            // Gather all entity definitions provided by the dependent modules which
            // are being removed.
            $module_entity_definitions = [];
            foreach ($this->entityTypeManager
                ->getDefinitions() as $entity_definition) {
                if (in_array($entity_definition->getProvider(), $dependencies['module'])) {
                    $module_entity_definitions[] = $entity_definition;
                }
            }
            // For all entity types provided by the uninstalled modules, remove any
            // configuration for those types.
            foreach ($module_entity_definitions as $module_entity_definition) {
                foreach ($this->getBundlesForEntityType($module_entity_definition->id()) as $bundle) {
                    $this->removeEntityTypeAndBundle($module_entity_definition->id(), $bundle);
                    $changed = TRUE;
                }
            }
        }
        return $changed;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getConfiguration() {
        $configuration = parent::getConfiguration();
        // Ensure that states and entity types are ordered consistently.
        ksort($configuration['states']);
        ksort($configuration['entity_types']);
        return $configuration;
    }
    
    /**
     * {@inheritdoc}
     */
    public function getInitialState($entity = NULL) {
        // Workflows are not tied to entities, but Content Moderation adds the
        // relationship between Workflows and entities. Content Moderation needs the
        // entity object to be able to determine the initial state based on
        // publishing status.
        if (!$entity instanceof ContentEntityInterface) {
            throw new \InvalidArgumentException('A content entity object must be supplied.');
        }
        if ($entity instanceof EntityPublishedInterface && !$entity->isNew()) {
            return $this->getState($entity->isPublished() ? 'published' : 'draft');
        }
        return $this->getState(!empty($this->configuration['default_moderation_state']) ? $this->configuration['default_moderation_state'] : 'draft');
    }

}

Members

Title Sort descending Modifiers Object type Summary Overriden Title Overrides
ContentModeration::$entityTypeBundleInfo protected property The entity type bundle info service.
ContentModeration::$entityTypeManager protected property The entity type manager.
ContentModeration::$moderationInfo protected property The moderation information service.
ContentModeration::addEntityTypeAndBundle public function Add an entity type ID / bundle ID to the workflow. Overrides ContentModerationInterface::addEntityTypeAndBundle
ContentModeration::appliesToEntityTypeAndBundle public function Checks if the workflow applies to the supplied entity type and bundle. Overrides ContentModerationInterface::appliesToEntityTypeAndBundle
ContentModeration::calculateDependencies public function Calculates dependencies for the configured plugin. Overrides WorkflowTypeBase::calculateDependencies
ContentModeration::create public static function Creates an instance of the plugin. Overrides ContainerFactoryPluginInterface::create
ContentModeration::defaultConfiguration public function Gets default configuration for this plugin. Overrides WorkflowTypeBase::defaultConfiguration
ContentModeration::getBundlesForEntityType public function Gets any bundles the workflow is applied to for the given entity type. Overrides ContentModerationInterface::getBundlesForEntityType
ContentModeration::getConfiguration public function Gets this plugin's configuration. Overrides WorkflowTypeBase::getConfiguration
ContentModeration::getEntityTypes public function Gets the entity types the workflow is applied to. Overrides ContentModerationInterface::getEntityTypes
ContentModeration::getInitialState public function Gets the initial state for the workflow. Overrides WorkflowTypeBase::getInitialState
ContentModeration::getState public function Gets a workflow state. Overrides WorkflowTypeBase::getState
ContentModeration::onDependencyRemoval public function Informs the plugin that a dependency of the workflow will be deleted. Overrides WorkflowTypeBase::onDependencyRemoval
ContentModeration::removeEntityTypeAndBundle public function Removes an entity type ID / bundle ID from the workflow. Overrides ContentModerationInterface::removeEntityTypeAndBundle
ContentModeration::workflowHasData public function Determines if the workflow is being has data associated with it. Overrides WorkflowTypeBase::workflowHasData
ContentModeration::workflowStateHasData public function Determines if the workflow state has data associated with it. Overrides WorkflowTypeBase::workflowStateHasData
ContentModeration::__construct public function Constructs a ContentModeration object. Overrides WorkflowTypeBase::__construct
PluginBase::$configuration protected property Configuration information passed into the plugin.
PluginBase::$pluginDefinition protected property The plugin implementation definition.
PluginBase::$pluginId protected property The plugin_id.
PluginBase::DERIVATIVE_SEPARATOR constant A string which is used to separate base plugin IDs from the derivative ID.
PluginBase::getBaseId public function Gets the base_plugin_id of the plugin instance. Overrides DerivativeInspectionInterface::getBaseId
PluginBase::getDerivativeId public function Gets the derivative_id of the plugin instance. Overrides DerivativeInspectionInterface::getDerivativeId
PluginBase::getPluginDefinition public function Gets the definition of the plugin implementation. Overrides PluginInspectionInterface::getPluginDefinition
PluginBase::getPluginId public function Gets the plugin_id of the plugin instance. Overrides PluginInspectionInterface::getPluginId
PluginBase::isConfigurable public function Determines if the plugin is configurable.
PluginWithFormsTrait::getFormClass public function
PluginWithFormsTrait::hasFormClass public function
StringTranslationTrait::$stringTranslation protected property The string translation service.
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
WorkflowTypeBase::addState public function Adds a state to the workflow. Overrides WorkflowTypeInterface::addState 1
WorkflowTypeBase::addTransition public function Adds a transition to the workflow. Overrides WorkflowTypeInterface::addTransition
WorkflowTypeBase::deleteState public function Deletes a state from the workflow. Overrides WorkflowTypeInterface::deleteState 1
WorkflowTypeBase::deleteTransition public function Deletes a transition. Overrides WorkflowTypeInterface::deleteTransition
WorkflowTypeBase::getNextWeight protected function Gets the weight for a new state or transition.
WorkflowTypeBase::getRequiredStates public function Gets the required states of workflow type. Overrides WorkflowTypeInterface::getRequiredStates 1
WorkflowTypeBase::getStates public function Gets state objects for the provided state IDs. Overrides WorkflowTypeInterface::getStates 1
WorkflowTypeBase::getTransition public function Gets a transition object for the provided transition ID. Overrides WorkflowTypeInterface::getTransition
WorkflowTypeBase::getTransitionFromStateToState public function Gets a transition from state to state. Overrides WorkflowTypeInterface::getTransitionFromStateToState
WorkflowTypeBase::getTransitionIdFromStateToState protected function Gets the transition ID from state to state.
WorkflowTypeBase::getTransitions public function Gets transition objects for the provided transition IDs. Overrides WorkflowTypeInterface::getTransitions
WorkflowTypeBase::getTransitionsForState public function Gets the transition IDs for a state for the provided direction. Overrides WorkflowTypeInterface::getTransitionsForState
WorkflowTypeBase::hasState public function Determines if the workflow has a state with the provided ID. Overrides WorkflowTypeInterface::hasState 1
WorkflowTypeBase::hasTransition public function Determines if a transition exists. Overrides WorkflowTypeInterface::hasTransition
WorkflowTypeBase::hasTransitionFromStateToState public function Determines if a transition from state to state exists. Overrides WorkflowTypeInterface::hasTransitionFromStateToState
WorkflowTypeBase::label public function Gets the label for the workflow type. Overrides WorkflowTypeInterface::label
WorkflowTypeBase::labelWeightMultisort protected static function Sort states or transitions by weight, label, and key.
WorkflowTypeBase::setConfiguration public function Sets the configuration for this plugin instance. Overrides ConfigurableInterface::setConfiguration
WorkflowTypeBase::setStateLabel public function Sets a state's label. Overrides WorkflowTypeInterface::setStateLabel 1
WorkflowTypeBase::setStateWeight public function Sets a state's weight value. Overrides WorkflowTypeInterface::setStateWeight 1
WorkflowTypeBase::setTransitionFromStates public function Sets a transition's from states. Overrides WorkflowTypeInterface::setTransitionFromStates
WorkflowTypeBase::setTransitionLabel public function Sets a transition's label. Overrides WorkflowTypeInterface::setTransitionLabel
WorkflowTypeBase::setTransitionWeight public function Sets a transition's weight. Overrides WorkflowTypeInterface::setTransitionWeight
WorkflowTypeBase::VALID_ID_REGEX constant A regex for matching a valid state/transition machine name.
WorkflowTypeInterface::PLUGIN_FORM_KEY constant The key of the global workflow plugin form.

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