5 * Defines the class for core actions.
6 * Belongs to the "action" operation type plugin.
9 class ViewsBulkOperationsAction
extends ViewsBulkOperationsBaseOperation
{
12 * Contains the options provided by the user in the configuration form.
16 public $formOptions = array();
19 * Returns the access bitmask for the operation, used for entity access checks.
21 public function getAccessMask() {
22 // Assume edit by default.
23 if (empty($this->operationInfo
['behavior'])) {
24 $this->operationInfo
['behavior'] = array('changes_property');
28 if (in_array('views_property', $this->operationInfo
['behavior'])) {
29 $mask |
= VBO_ACCESS_OP_VIEW
;
31 if (in_array('changes_property', $this->operationInfo
['behavior'])) {
32 $mask |
= VBO_ACCESS_OP_UPDATE
;
34 if (in_array('creates_property', $this->operationInfo
['behavior'])) {
35 $mask |
= VBO_ACCESS_OP_CREATE
;
37 if (in_array('deletes_property', $this->operationInfo
['behavior'])) {
38 $mask |
= VBO_ACCESS_OP_DELETE
;
44 * Returns whether the provided account has access to execute the operation.
48 public function access($account) {
49 // Use actions_permissions if enabled.
50 if (module_exists('actions_permissions')) {
51 $perm = actions_permissions_get_perm($this->operationInfo
['label'], $this->operationInfo
['key']);
52 if (!user_access($perm, $account)) {
56 // Check against additional permissions.
57 if (!empty($this->operationInfo
['permissions'])) {
58 foreach ($this->operationInfo
['permissions'] as $perm) {
59 if (!user_access($perm, $account)) {
69 * Returns the configuration form for the operation.
70 * Only called if the operation is declared as configurable.
75 * An array containing the current state of the form.
77 * An array of related data provided by the caller.
79 public function form($form, &$form_state, array $context) {
80 // Some modules (including this one) place their action callbacks
81 // into separate files. At this point those files might no longer be
82 // included due to an #ajax rebuild, so we call actions_list() to trigger
83 // inclusion. The same thing is done by actions_do() on execute.
86 $context['settings'] = $this->getAdminOption('settings', array());
87 $form_callback = $this->operationInfo
['callback'] . '_form';
88 return $form_callback($context, $form_state);
92 * Validates the configuration form.
93 * Only called if the operation is declared as configurable.
98 * An array containing the current state of the form.
100 public function formValidate($form, &$form_state) {
101 // Some modules (including this one) place their action callbacks
102 // into separate files. At this point those files might no longer be
103 // included due to a page reload, so we call actions_list() to trigger
104 // inclusion. The same thing is done by actions_do() on execute.
107 $validation_callback = $this->operationInfo
['callback'] . '_validate';
108 if (function_exists($validation_callback)) {
109 $validation_callback($form, $form_state);
114 * Handles the submitted configuration form.
115 * This is where the operation can transform and store the submitted data.
116 * Only called if the operation is declared as configurable.
121 * An array containing the current state of the form.
123 public function formSubmit($form, &$form_state) {
124 // Some modules (including this one) place their action callbacks
125 // into separate files. At this point those files might no longer be
126 // included due to a page reload, so we call actions_list() to trigger
127 // inclusion. The same thing is done by actions_do() on execute.
130 $submit_callback = $this->operationInfo
['callback'] . '_submit';
131 $this->formOptions
= $submit_callback($form, $form_state);
135 * Returns the admin options form for the operation.
137 * The admin options form is embedded into the VBO field settings and used
138 * to configure operation behavior. The options can later be fetched
139 * through the getAdminOption() method.
142 * The dom path to the level where the admin options form is embedded.
143 * Needed for #dependency.
144 * @param $field_handler
145 * The Views field handler object for the VBO field.
147 public function adminOptionsForm($dom_id, $field_handler) {
148 $form = parent
::adminOptionsForm($dom_id, $field_handler);
150 $settings_form_callback = $this->operationInfo
['callback'] . '_views_bulk_operations_form';
151 if (function_exists($settings_form_callback)) {
152 $settings = $this->getAdminOption('settings', array());
154 $form['settings'] = array(
155 '#type' => 'fieldset',
156 '#title' => t('Operation settings'),
157 '#collapsible' => TRUE,
158 '#dependency' => array(
159 $dom_id . '-selected' => array(1),
162 $settings_dom_id = $dom_id . '-settings';
163 $form['settings'] +
= $settings_form_callback($settings, $this->entityType
, $settings_dom_id);
170 * Validates the admin options form.
173 * The admin options form.
175 * An array containing the current state of the form. Note that this array
176 * is constructed by the VBO views field handler, so it's not a real form
177 * state, it contains only the 'values' key.
178 * @param $error_element_base
179 * The base to prepend to field names when using form_set_error().
180 * Needed because the admin settings form is embedded into a bigger form.
182 public function adminOptionsFormValidate($form, &$form_state, $error_element_base) {
183 parent
::adminOptionsFormValidate($form, $form_state, $error_element_base);
185 if (!empty($form['settings'])) {
186 $settings_validation_callback = $this->operationInfo
['callback'] . '_views_bulk_operations_form_validate';
187 if (function_exists($settings_validation_callback)) {
188 $fake_form = $form['settings'];
189 $fake_form_state = array('values' => &$form_state['values']['settings']);
190 $error_element_base .= 'settings][';
192 $settings_validation_callback($fake_form, $fake_form_state, $error_element_base);
198 * Handles the submitted admin options form.
199 * Note that there is no need to handle saving the options, that is done
200 * by the VBO views field handler, which also injects the options into the
201 * operation object upon instantiation.
204 * The admin options form.
206 * An array containing the current state of the form. Note that this array
207 * is constructed by the VBO views field handler, so it's not a real form
208 * state, it contains only the 'values' key.
210 public function adminOptionsFormSubmit($form, &$form_state) {
211 parent
::adminOptionsFormSubmit($form, $form_state);
213 if (!empty($form['settings'])) {
214 $settings_submit_callback = $this->operationInfo
['callback'] . '_views_bulk_operations_form_submit';
215 if (function_exists($settings_submit_callback)) {
216 $fake_form = $form['settings'];
217 $fake_form_state = array('values' => &$form_state['values']['settings']);
219 $settings_submit_callback($form, $form_state);
225 * Returns whether the operation needs the full selected views rows to be
226 * passed to execute() as a part of $context.
228 public function needsRows() {
229 return !empty($this->operationInfo
['pass rows']);
233 * Executes the selected operation on the provided data.
236 * The data to to operate on. An entity or an array of entities.
238 * An array of related data (selected views rows, etc).
240 public function execute($data, array $context) {
241 $context['entity_type'] = $this->entityType
;
242 $context['settings'] = $this->getAdminOption('settings', array());
243 $context +
= $this->formOptions
;
244 $context +
= $this->operationInfo
['parameters'];
245 // Actions provided by the Drupal system module require the entity to be
246 // present in $context, keyed by entity type.
247 if (is_object($data)) {
248 $context[$this->entityType
] = $data;
251 actions_do($this->operationInfo
['callback'], $data, $context);
253 // The action might need to have its entities saved after execution.
254 if (in_array('changes_property', $this->operationInfo
['behavior'])) {
255 $data = is_array($data) ?
$data : array($data);
256 foreach ($data as $entity) {
257 entity_save($this->entityType
, $entity);