Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / core / lib / Drupal / Core / Action / Plugin / Action / SaveAction.php
1 <?php
2
3 namespace Drupal\Core\Action\Plugin\Action;
4
5 use Drupal\Component\Datetime\TimeInterface;
6 use Drupal\Core\Entity\EntityTypeManagerInterface;
7 use Drupal\Core\Session\AccountInterface;
8 use Symfony\Component\DependencyInjection\ContainerInterface;
9
10 /**
11  * Provides an action that can save any entity.
12  *
13  * @Action(
14  *   id = "entity:save_action",
15  *   action_label = @Translation("Save"),
16  *   deriver = "Drupal\Core\Action\Plugin\Action\Derivative\EntityChangedActionDeriver",
17  * )
18  */
19 class SaveAction extends EntityActionBase {
20
21   /**
22    * The time service.
23    *
24    * @var \Drupal\Component\Datetime\TimeInterface
25    */
26   protected $time;
27
28   /**
29    * Constructs a SaveAction object.
30    *
31    * @param mixed[] $configuration
32    *   A configuration array containing information about the plugin instance.
33    * @param string $plugin_id
34    *   The plugin ID for the plugin instance.
35    * @param mixed $plugin_definition
36    *   The plugin implementation definition.
37    * @param \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager
38    *   The entity type manager.
39    * @param \Drupal\Component\Datetime\TimeInterface $time
40    *   The time service.
41    */
42   public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityTypeManagerInterface $entity_type_manager, TimeInterface $time) {
43     parent::__construct($configuration, $plugin_id, $plugin_definition, $entity_type_manager);
44     $this->time = $time;
45   }
46
47   /**
48    * {@inheritdoc}
49    */
50   public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
51     return new static(
52       $configuration,
53       $plugin_id,
54       $plugin_definition,
55       $container->get('entity_type.manager'),
56       $container->get('datetime.time')
57     );
58   }
59
60   /**
61    * {@inheritdoc}
62    */
63   public function execute($entity = NULL) {
64     $entity->setChangedTime($this->time->getRequestTime())->save();
65   }
66
67   /**
68    * {@inheritdoc}
69    */
70   public function access($object, AccountInterface $account = NULL, $return_as_object = FALSE) {
71     // It's not necessary to check the changed field access here, because
72     // Drupal\Core\Field\ChangedFieldItemList would anyway return 'not allowed'.
73     // Also changing the changed field value is only a workaround to trigger an
74     // entity resave. Without a field change, this would not be possible.
75     /** @var \Drupal\Core\Entity\EntityInterface $object */
76     return $object->access('update', $account, $return_as_object);
77   }
78
79 }