You are here

protected function ModerationStateFieldItemList::getModerationStateId in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList::getModerationStateId()

Gets the moderation state ID linked to a content entity revision.

Return value

string|null The moderation state ID linked to a content entity revision.

1 call to ModerationStateFieldItemList::getModerationStateId()
ModerationStateFieldItemList::computeValue in core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php
Computes the values for an item list.

File

core/modules/content_moderation/src/Plugin/Field/ModerationStateFieldItemList.php, line 42

Class

ModerationStateFieldItemList
A computed field that provides a content entity's moderation state.

Namespace

Drupal\content_moderation\Plugin\Field

Code

protected function getModerationStateId() {
  $entity = $this
    ->getEntity();

  /** @var \Drupal\content_moderation\ModerationInformationInterface $moderation_info */
  $moderation_info = \Drupal::service('content_moderation.moderation_information');
  if (!$moderation_info
    ->shouldModerateEntitiesOfBundle($entity
    ->getEntityType(), $entity
    ->bundle())) {
    return NULL;
  }

  // Existing entities will have a corresponding content_moderation_state
  // entity associated with them.
  if (!$entity
    ->isNew() && ($content_moderation_state = $this
    ->loadContentModerationStateRevision($entity))) {
    return $content_moderation_state->moderation_state->value;
  }

  // It is possible that the bundle does not exist at this point. For example,
  // the node type form creates a fake Node entity to get default values.
  // @see \Drupal\node\NodeTypeForm::form()
  $workflow = $moderation_info
    ->getWorkFlowForEntity($entity);
  return $workflow ? $workflow
    ->getTypePlugin()
    ->getInitialState($entity)
    ->id() : NULL;
}