5 * Defines the base class for operations.
8 abstract class ViewsBulkOperationsBaseOperation
{
11 * The id of the operation.
13 * Composed of the operation_type plugin name and the operation key,
14 * divided by '::'. For example: action::node_publish_action.
19 * The entity type that the operation is operating on.
21 * Not the same as $operationInfo['type'] since that value can be just
22 * "entity", which means "available to every entity type".
27 * Contains information about the current operation, as generated
28 * by the "list callback" function in the plugin file.
32 protected $operationInfo;
35 * Contains the options set by the admin for the current operation.
39 protected $adminOptions;
42 * Constructs an operation object.
45 * The id of the operation.
47 * The entity type that the operation is operating on.
48 * @param $operationInfo
49 * An array of information about the operation.
50 * @param $adminOptions
51 * An array of options set by the admin.
53 public function __construct($operationId, $entityType, array $operationInfo, array $adminOptions) {
54 $this->operationId
= $operationId;
55 $this->entityType
= $entityType;
56 $this->operationInfo
= $operationInfo;
57 $this->adminOptions
= $adminOptions;
61 * Returns the value of an admin option.
63 public function getAdminOption($key, $default = NULL) {
64 return isset($this->adminOptions
[$key]) ?
$this->adminOptions
[$key] : $default;
68 * Returns the access bitmask for the operation, used for entity access checks.
70 public function getAccessMask() {
71 // Assume edit by default.
72 return VBO_ACCESS_OP_UPDATE
;
76 * Returns the id of the operation.
78 public function id() {
79 return $this->operationId
;
83 * Returns the name of the operation_type plugin that provides the operation.
85 public function type() {
86 return $this->operationInfo
['operation_type'];
90 * Returns the human-readable name of the operation, meant to be shown
93 public function label() {
94 $admin_label = $this->getAdminOption('label');
95 if (!empty($admin_label)) {
96 $label = t($admin_label);
99 // If the admin didn't specify any label, fallback to the default one.
100 $label = $this->operationInfo
['label'];
106 * Returns the human-readable name of the operation, meant to be shown
109 public function adminLabel() {
110 return $this->operationInfo
['label'];
114 * Returns whether the operation is configurable. Used to determine
115 * whether the operation's form methods should be invoked.
117 public function configurable() {
118 return !empty($this->operationInfo
['configurable']);
122 * Returns whether the provided account has access to execute the operation.
126 public function access($account) {
131 * Returns the configuration form for the operation.
132 * Only called if the operation is declared as configurable.
137 * An array containing the current state of the form.
139 * An array of related data provided by the caller.
141 abstract function form($form, &$form_state, array $context);
144 * Validates the configuration form.
145 * Only called if the operation is declared as configurable.
150 * An array containing the current state of the form.
152 abstract function formValidate($form, &$form_state);
155 * Handles the submitted configuration form.
156 * This is where the operation can transform and store the submitted data.
157 * Only called if the operation is declared as configurable.
162 * An array containing the current state of the form.
164 abstract function formSubmit($form, &$form_state);
167 * Returns the admin options form for the operation.
169 * The admin options form is embedded into the VBO field settings and used
170 * to configure operation behavior. The options can later be fetched
171 * through the getAdminOption() method.
174 * The dom path to the level where the admin options form is embedded.
175 * Needed for #dependency.
176 * @param $field_handler
177 * The Views field handler object for the VBO field.
179 public function adminOptionsForm($dom_id, $field_handler) {
180 $label = $this->getAdminOption('label', '');
183 $form['override_label'] = array(
184 '#type' => 'checkbox',
185 '#title' => t('Override label'),
186 '#default_value' => $label !== '',
187 '#dependency' => array(
188 $dom_id . '-selected' => array(1),
191 $form['label'] = array(
192 '#type' => 'textfield',
193 '#title' => t('Provide label'),
194 '#title_display' => 'invisible',
195 '#default_value' => $label,
196 '#dependency' => array(
197 $dom_id . '-selected' => array(1),
198 $dom_id . '-override-label' => array(1),
200 '#dependency_count' => 2,
207 * Validates the admin options form.
210 * The admin options form.
212 * An array containing the current state of the form. Note that this array
213 * is constructed by the VBO views field handler, so it's not a real form
214 * state, it contains only the 'values' key.
215 * @param $error_element_base
216 * The base to prepend to field names when using form_set_error().
217 * Needed because the admin options form is embedded into a bigger form.
219 public function adminOptionsFormValidate($form, &$form_state, $error_element_base) {
220 // No need to do anything, but make the function have a body anyway
221 // so that it's callable by overriding methods.
225 * Handles the submitted admin options form.
226 * Note that there is no need to handle saving the options, that is done
227 * by the VBO views field handler, which also injects the options into the
228 * operation object upon instantiation.
231 * The admin options form.
233 * An array containing the current state of the form. Note that this array
234 * is constructed by the VBO views field handler, so it's not a real form
235 * state, it contains only the 'values' key.
237 public function adminOptionsFormSubmit($form, &$form_state) {
238 // If the "Override label" checkbox was deselected, clear the entered value.
239 if (empty($form_state['values']['override_label'])) {
240 $form_state['values']['label'] = '';
245 * Returns whether the selected entities should be aggregated
246 * (loaded in bulk and passed in together).
247 * To be avoided if possible, since aggregation makes it impossible to use
248 * Batch API or the Drupal Queue for execution.
250 public function aggregate() {
251 return !empty($this->operationInfo
['aggregate']);
255 * Returns whether the operation needs the full selected views rows to be
256 * passed to execute() as a part of $context.
258 public function needsRows() {
263 * Executes the selected operation on the provided data.
266 * The data to to operate on. An entity or an array of entities.
268 * An array of related data (selected views rows, etc).
270 abstract function execute($data, array $context);