You are here

class ThunderNodeForm in Thunder 8.3

Same name and namespace in other branches
  1. 8.5 modules/thunder_article/src/Form/ThunderNodeForm.php \Drupal\thunder_article\Form\ThunderNodeForm
  2. 8.2 modules/thunder_article/src/Form/ThunderNodeForm.php \Drupal\thunder_article\Form\ThunderNodeForm
  3. 8.4 modules/thunder_article/src/Form/ThunderNodeForm.php \Drupal\thunder_article\Form\ThunderNodeForm
  4. 6.2.x modules/thunder_article/src/Form/ThunderNodeForm.php \Drupal\thunder_article\Form\ThunderNodeForm
  5. 6.0.x modules/thunder_article/src/Form/ThunderNodeForm.php \Drupal\thunder_article\Form\ThunderNodeForm
  6. 6.1.x modules/thunder_article/src/Form/ThunderNodeForm.php \Drupal\thunder_article\Form\ThunderNodeForm

Base for handler for node add/edit forms.

Hierarchy

Expanded class hierarchy of ThunderNodeForm

1 file declares its use of ThunderNodeForm
thunder_article.module in modules/thunder_article/thunder_article.module
Thunder Article module hooks.

File

modules/thunder_article/src/Form/ThunderNodeForm.php, line 20

Namespace

Drupal\thunder_article\Form
View source
class ThunderNodeForm implements ContainerInjectionInterface {
  use StringTranslationTrait;

  /**
   * The current user.
   *
   * @var \Drupal\Core\Session\AccountInterface
   */
  protected $currentUser;

  /**
   * The messenger service.
   *
   * @var \Drupal\Core\Messenger\MessengerInterface
   */
  protected $messenger;

  /**
   * The request object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
   * The node revision access check service.
   *
   * @var \Drupal\node\Access\NodeRevisionAccessCheck
   */
  protected $nodeRevisionAccess;

  /**
   * The moderation information service.
   *
   * @var \Drupal\content_moderation\ModerationInformationInterface
   */
  protected $moderationInfo;

  /**
   * The entity type manager.
   *
   * @var \Drupal\Core\Entity\EntityTypeManagerInterface
   */
  protected $entityTypeManager;

  /**
   * Constructs a NodeForm object.
   *
   * @param \Drupal\Core\Session\AccountInterface $current_user
   *   The current user.
   * @param \Drupal\Core\Messenger\MessengerInterface $messenger
   *   The messenger service.
   * @param \Symfony\Component\HttpFoundation\RequestStack $requestStack
   *   The request stack service.
   * @param \Drupal\node\Access\NodeRevisionAccessCheck $node_revision_access
   *   The node revision access check service.
   * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
   *   The entity type manager.
   * @param \Drupal\content_moderation\ModerationInformationInterface $moderationInfo
   *   (optional) The moderation info service. The optionality is important
   *   otherwise this form becomes dependent on the content_moderation module.
   */
  public function __construct(AccountInterface $current_user, MessengerInterface $messenger, RequestStack $requestStack, NodeRevisionAccessCheck $node_revision_access, EntityTypeManagerInterface $entity_type_manager, ModerationInformationInterface $moderationInfo = NULL) {
    $this->currentUser = $current_user;
    $this->messenger = $messenger;
    $this->request = $requestStack
      ->getCurrentRequest();
    $this->nodeRevisionAccess = $node_revision_access;
    $this->entityTypeManager = $entity_type_manager;
    $this->moderationInfo = $moderationInfo;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
    return new static($container
      ->get('current_user'), $container
      ->get('messenger'), $container
      ->get('request_stack'), $container
      ->get('access_check.node.revision'), $container
      ->get('entity_type.manager'), $container
      ->has('content_moderation.moderation_information') ? $container
      ->get('content_moderation.moderation_information') : NULL);
  }

  /**
   * {@inheritdoc}
   */
  public function formAlter(array &$form, FormStateInterface $form_state) {

    /** @var \Drupal\Core\Entity\ContentEntityFormInterface $form_object */
    $form_object = $form_state
      ->getFormObject();

    /** @var \Drupal\node\NodeInterface $entity */
    $entity = $form_object
      ->getEntity();
    $storage = $this->entityTypeManager
      ->getStorage($entity
      ->getEntityTypeId());
    $latest_revision_id = $storage
      ->getLatestTranslationAffectedRevisionId($entity
      ->id(), $entity
      ->language()
      ->getId());
    if ($latest_revision_id !== NULL && $this->moderationInfo && $this->moderationInfo
      ->hasPendingRevision($entity)) {
      $this->messenger
        ->addWarning($this
        ->t('This %entity_type has unpublished changes from user %user.', [
        '%entity_type' => $entity
          ->get('type')->entity
          ->label(),
        '%user' => $entity
          ->getRevisionUser()
          ->label(),
      ]));
    }
    $form['actions'] = array_merge($form['actions'], $this
      ->actions($entity));
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  protected function actions($entity) {
    $storage = $this->entityTypeManager
      ->getStorage($entity
      ->getEntityTypeId());
    $latest_revision_id = $storage
      ->getLatestTranslationAffectedRevisionId($entity
      ->id(), $entity
      ->language()
      ->getId());
    if ($latest_revision_id == NULL || !$this->moderationInfo || !$this->moderationInfo
      ->isModeratedEntity($entity)) {
      return [];
    }
    $state = $this->moderationInfo
      ->getWorkflowForEntity($entity)
      ->getTypePlugin()
      ->getState($entity->moderation_state->value);
    $element['status'] = [
      '#type' => 'item',
      '#markup' => $entity
        ->isNew() || !$this->moderationInfo
        ->isDefaultRevisionPublished($entity) ? $this
        ->t('of unpublished @entity_type', [
        '@entity_type' => strtolower($entity->type->entity
          ->label()),
      ]) : $this
        ->t('of published @entity_type', [
        '@entity_type' => strtolower($entity->type->entity
          ->label()),
      ]),
      '#weight' => 200,
      '#wrapper_attributes' => [
        'class' => [
          'status',
        ],
      ],
      '#access' => !$state
        ->isDefaultRevisionState(),
    ];
    $element['moderation_state_current'] = [
      '#type' => 'item',
      '#markup' => $state
        ->label(),
      '#weight' => 210,
      '#wrapper_attributes' => [
        'class' => [
          'status',
          $state
            ->id(),
        ],
      ],
    ];
    if ($this->moderationInfo
      ->hasPendingRevision($entity)) {
      $route_info = Url::fromRoute('node.revision_revert_default_confirm', [
        'node' => $entity
          ->id(),
        'node_revision' => $entity
          ->getRevisionId(),
      ]);
      if ($this->request->query
        ->has('destination')) {
        $query = $route_info
          ->getOption('query');
        $query['destination'] = $this->request->query
          ->get('destination');
        $route_info
          ->setOption('query', $query);
      }
      $element['revert_to_default'] = [
        '#type' => 'link',
        '#title' => $this
          ->t('Revert to default revision'),
        '#access' => $this->nodeRevisionAccess
          ->checkAccess($entity, $this->currentUser, 'update'),
        '#weight' => 101,
        '#attributes' => [
          'class' => [
            'button',
            'button--danger',
          ],
        ],
      ];
      $element['revert_to_default']['#url'] = $route_info;
    }
    return $element;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
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.
ThunderNodeForm::$currentUser protected property The current user.
ThunderNodeForm::$entityTypeManager protected property The entity type manager.
ThunderNodeForm::$messenger protected property The messenger service.
ThunderNodeForm::$moderationInfo protected property The moderation information service.
ThunderNodeForm::$nodeRevisionAccess protected property The node revision access check service.
ThunderNodeForm::$request protected property The request object.
ThunderNodeForm::actions protected function
ThunderNodeForm::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create
ThunderNodeForm::formAlter public function
ThunderNodeForm::__construct public function Constructs a NodeForm object.