-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathEntitySave.php
More file actions
106 lines (94 loc) · 2.68 KB
/
EntitySave.php
File metadata and controls
106 lines (94 loc) · 2.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* @file
* Contains \Drupal\rules\Plugin\RulesAction\EntitySave.
*/
namespace Drupal\rules\Plugin\RulesAction;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\rules\Core\RulesActionBase;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
/**
* Provides a 'Save entity' action.
*
* @RulesAction(
* id = "rules_entity_save",
* deriver = "Drupal\rules\Plugin\RulesAction\EntitySaveDeriver",
* )
*
* @todo: Add access callback information from Drupal 7.
*/
class EntitySave extends RulesActionBase implements ContainerFactoryPluginInterface{
/**
* The entity storage service.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $storage;
/**
* The entity type id.
*
* @var string
*/
protected $entityTypeId;
/**
* Flag that indicates if the entity should be auto-saved later.
*
* @var bool
*/
protected $saveLater = TRUE;
/**
* Constructs an EntitySave 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\EntityStorageInterface $storage
* The entity storage service.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, EntityStorageInterface $storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->storage = $storage;
$this->entityTypeId = $plugin_definition['entity_type_id'];
}
/**
* {@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')->getStorage($plugin_definition['entity_type_id'])
);
}
/**
* Saves the Entity.
*
* @param \Drupal\Core\Entity\EntityInterface $entity
* The entity to be saved.
* @param bool $immediate
* (optional) Save the entity immediately.
*/
protected function doExecute(EntityInterface $entity, $immediate) {
// We only need to do something here if the immediate flag is set, otherwise
// the entity will be auto-saved after the execution.
if ((bool) $immediate) {
$entity->save();
$this->saveLater = FALSE;
}
}
/**
* {@inheritdoc}
*/
public function autoSaveContext() {
if ($this->saveLater) {
return ['entity'];
}
return [];
}
}