1<?php
2
3namespace Drupal\Core\Action\Plugin\Action;
4
5use Drupal\Core\Session\AccountInterface;
6
7/**
8 * Publishes an entity.
9 *
10 * @Action(
11 *   id = "entity:publish_action",
12 *   action_label = @Translation("Publish"),
13 *   deriver = "Drupal\Core\Action\Plugin\Action\Derivative\EntityPublishedActionDeriver",
14 * )
15 */
16class PublishAction extends EntityActionBase {
17
18  /**
19   * {@inheritdoc}
20   */
21  public function execute($entity = NULL) {
22    $entity->setPublished()->save();
23  }
24
25  /**
26   * {@inheritdoc}
27   */
28  public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
29    $key = $object->getEntityType()->getKey('published');
30
31    /** @var \Drupal\Core\Entity\EntityInterface $object */
32    $result = $object->access('update', $account, TRUE)
33      ->andIf($object->$key->access('edit', $account, TRUE));
34
35    return $return_as_object ? $result : $result->isAllowed();
36  }
37
38}
39