modify.action.inc 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  1. <?php
  2. /**
  3. * @file VBO action to modify entity values (properties and fields).
  4. */
  5. // Specifies that all available values should be shown to the user for editing.
  6. define('VBO_MODIFY_ACTION_ALL', '_all_');
  7. function views_bulk_operations_modify_action_info() {
  8. return array('views_bulk_operations_modify_action' => array(
  9. 'type' => 'entity',
  10. 'label' => t('Modify entity values'),
  11. 'behavior' => array('changes_property'),
  12. // This action only works when invoked through VBO. That's why it's
  13. // declared as non-configurable to prevent it from being shown in the
  14. // "Create an advanced action" dropdown on admin/config/system/actions.
  15. 'configurable' => FALSE,
  16. 'vbo_configurable' => TRUE,
  17. 'triggers' => array('any'),
  18. ));
  19. }
  20. /**
  21. * Action function.
  22. *
  23. * Goes through new values and uses them to modify the passed entity by either
  24. * replacing the existing values, or appending to them (based on user input).
  25. */
  26. function views_bulk_operations_modify_action($entity, $context) {
  27. list(,,$bundle_name) = entity_extract_ids($context['entity_type'], $entity);
  28. // Handle Field API fields.
  29. if (!empty($context['selected']['bundle_' . $bundle_name])) {
  30. // The pseudo entity is cloned so that changes to it don't get carried
  31. // over to the next execution.
  32. $pseudo_entity = clone $context['entities'][$bundle_name];
  33. foreach ($context['selected']['bundle_' . $bundle_name] as $key) {
  34. // Get this field's language. We can just pull it from the pseudo entity
  35. // as it was created using field_attach_form and entity_language so it's
  36. // already been figured out if this field is translatable or not and
  37. // applied the appropriate language code to the field
  38. $language = key($pseudo_entity->{$key});
  39. // Replace any tokens that might exist in the field columns.
  40. foreach ($pseudo_entity->{$key}[$language] as $delta => &$item) {
  41. foreach ($item as $column => $value) {
  42. if (is_string($value)) {
  43. $item[$column] = token_replace($value, array($context['entity_type'] => $entity), array('sanitize' => FALSE));
  44. }
  45. }
  46. }
  47. if (in_array($key, $context['append']['bundle_' . $bundle_name]) && !empty($entity->$key)) {
  48. $entity->{$key}[$language] = array_merge($entity->{$key}[$language], $pseudo_entity->{$key}[$language]);
  49. // Check if we breached cardinality, and notify the user.
  50. $field_info = field_info_field($key);
  51. $field_count = count($entity->{$key}[$language]);
  52. if ($field_info['cardinality'] != FIELD_CARDINALITY_UNLIMITED && $field_count > $field_info['cardinality']) {
  53. $entity_label = entity_label($context['entity_type'], $entity);
  54. $warning = t('Tried to set !field_count values for field !field_name that supports a maximum of !cardinality.',
  55. array('!field_count' => $field_count,
  56. '!field_name' => $field_info['field_name'],
  57. '!cardinality' => $field_info['cardinality']));
  58. drupal_set_message($warning, 'warning', FALSE);
  59. }
  60. // Prevent storing duplicate references.
  61. if (strpos($field_info['type'], 'reference') !== FALSE) {
  62. $entity->{$key}[$language] = array_unique($entity->{$key}[LANGUAGE_NONE], SORT_REGULAR);
  63. }
  64. }
  65. else {
  66. $entity->{$key}[$language] = $pseudo_entity->{$key}[$language];
  67. }
  68. }
  69. }
  70. // Handle properties.
  71. if (!empty($context['selected']['properties'])) {
  72. // Use the wrapper to set property values, since some properties need
  73. // additional massaging by their setter callbacks.
  74. // The wrapper will automatically modify $entity itself.
  75. $wrapper = entity_metadata_wrapper($context['entity_type'], $entity);
  76. foreach ($context['selected']['properties'] as $key) {
  77. if (!$wrapper->$key->access('update')) {
  78. // No access.
  79. continue;
  80. }
  81. if (in_array($key, $context['append']['properties'])) {
  82. $old_values = $wrapper->$key->value();
  83. $wrapper->$key->set($context['properties'][$key]);
  84. $new_values = $wrapper->{$key}->value();
  85. $all_values = array_merge($old_values, $new_values);
  86. $wrapper->$key->set($all_values);
  87. }
  88. else {
  89. $value = $context['properties'][$key];
  90. if (is_string($value)) {
  91. $value = token_replace($value, array($context['entity_type'] => $entity), array('sanitize' => FALSE));
  92. }
  93. $wrapper->$key->set($value);
  94. }
  95. }
  96. }
  97. }
  98. /**
  99. * Action form function.
  100. *
  101. * Displays form elements for properties acquired through Entity Metadata
  102. * (hook_entity_property_info()), as well as field widgets for each
  103. * entity bundle, as provided by field_attach_form().
  104. */
  105. function views_bulk_operations_modify_action_form($context, &$form_state) {
  106. // This action form uses admin-provided settings. If they were not set, pull the defaults now.
  107. if (!isset($context['settings'])) {
  108. $context['settings'] = views_bulk_operations_modify_action_views_bulk_operations_form_options();
  109. }
  110. $form_state['entity_type'] = $entity_type = $context['entity_type'];
  111. // For Field API integration to work, a pseudo-entity is constructed for each
  112. // bundle that has fields available for editing.
  113. // The entities then get passed to Field API functions
  114. // (field_attach_form(), field_attach_form_validate(), field_attach_submit()),
  115. // and filled with form data.
  116. // After submit, the pseudo-entities get passed to the actual action
  117. // (views_bulk_operations_modify_action()) which copies the data from the
  118. // relevant pseudo-entity constructed here to the actual entity being modified.
  119. $form_state['entities'] = array();
  120. $info = entity_get_info($entity_type);
  121. $properties = _views_bulk_operations_modify_action_get_properties($entity_type, $context['settings']['display_values']);
  122. $bundles = _views_bulk_operations_modify_action_get_bundles($entity_type, $context);
  123. $form['#attached']['css'][] = drupal_get_path('module', 'views_bulk_operations') . '/css/modify.action.css';
  124. $form['#tree'] = TRUE;
  125. if (!empty($properties)) {
  126. $form['properties'] = array(
  127. '#type' => 'fieldset',
  128. '#title' => t('Properties'),
  129. );
  130. $form['properties']['show_value'] = array(
  131. '#suffix' => '<div class="clearfix"></div>',
  132. );
  133. foreach ($properties as $key => $property) {
  134. $form['properties']['show_value'][$key] = array(
  135. '#type' => 'checkbox',
  136. '#title' => $property['label'],
  137. );
  138. $determined_type = ($property['type'] == 'boolean') ? 'checkbox' : 'textfield';
  139. $form['properties'][$key] = array(
  140. '#type' => $determined_type,
  141. '#title' => $property['label'],
  142. '#description' => $property['description'],
  143. '#states' => array(
  144. 'visible' => array(
  145. '#edit-properties-show-value-' . str_replace('_', '-', $key) => array('checked' => TRUE),
  146. ),
  147. ),
  148. );
  149. // The default #maxlength for textfields is 128, while most varchar
  150. // columns hold 255 characters, which makes it a saner default here.
  151. if ($determined_type == 'textfield') {
  152. $form['properties'][$key]['#maxlength'] = 255;
  153. }
  154. if (!empty($property['options list'])) {
  155. $form['properties'][$key]['#type'] = 'select';
  156. $form['properties'][$key]['#options'] = $property['options list']($key, array());
  157. if ($property['type'] == 'list') {
  158. $form['properties'][$key]['#type'] = 'checkboxes';
  159. $form['properties']['_append::' . $key] = array(
  160. '#type' => 'checkbox',
  161. '#title' => t('Add new value(s) to %label, instead of overwriting the existing values.', array('%label' => $property['label'])),
  162. '#states' => array(
  163. 'visible' => array(
  164. '#edit-properties-show-value-' . $key => array('checked' => TRUE),
  165. ),
  166. ),
  167. );
  168. }
  169. }
  170. }
  171. }
  172. // Going to need this for multilingual nodes
  173. global $language;
  174. foreach ($bundles as $bundle_name => $bundle) {
  175. $bundle_key = $info['entity keys']['bundle'];
  176. $default_values = array();
  177. // If the bundle key exists, it must always be set on an entity.
  178. if (!empty($bundle_key)) {
  179. $default_values[$bundle_key] = $bundle_name;
  180. }
  181. $default_values['language'] = $language->language;
  182. $entity = entity_create($context['entity_type'], $default_values);
  183. $form_state['entities'][$bundle_name] = $entity;
  184. // Show the more detailed label only if the entity type has multiple bundles.
  185. // Otherwise, it would just be confusing.
  186. if (count($info['bundles']) > 1) {
  187. $label = t('Fields for @bundle_key @label', array('@bundle_key' => $bundle_key, '@label' => $bundle['label']));
  188. }
  189. else {
  190. $label = t('Fields');
  191. }
  192. $form_key = 'bundle_' . $bundle_name;
  193. $form[$form_key] = array(
  194. '#type' => 'fieldset',
  195. '#title' => $label,
  196. '#parents' => array($form_key),
  197. );
  198. field_attach_form($context['entity_type'], $entity, $form[$form_key], $form_state, entity_language($context['entity_type'], $entity));
  199. // Now that all the widgets have been added, sort them by #weight.
  200. // This ensures that they will stay in the correct order when they get
  201. // assigned new weights.
  202. uasort($form[$form_key], 'element_sort');
  203. $display_values = $context['settings']['display_values'];
  204. $instances = field_info_instances($entity_type, $bundle_name);
  205. $weight = 0;
  206. foreach (element_get_visible_children($form[$form_key]) as $field_name) {
  207. // For our use case it makes no sense for any field widget to be required.
  208. if (isset($form[$form_key][$field_name]['#language'])) {
  209. $field_language = $form[$form_key][$field_name]['#language'];
  210. _views_bulk_operations_modify_action_unset_required($form[$form_key][$field_name][$field_language]);
  211. }
  212. // The admin has specified which fields to display, but this field didn't
  213. // make the cut. Hide it with #access => FALSE and move on.
  214. if (empty($display_values[VBO_MODIFY_ACTION_ALL]) && empty($display_values[$bundle_name . '::' . $field_name])) {
  215. $form[$form_key][$field_name]['#access'] = FALSE;
  216. continue;
  217. }
  218. if (isset($instances[$field_name])) {
  219. $field = $instances[$field_name];
  220. $form[$form_key]['show_value'][$field_name] = array(
  221. '#type' => 'checkbox',
  222. '#title' => $field['label'],
  223. );
  224. $form[$form_key][$field_name]['#states'] = array(
  225. 'visible' => array(
  226. '#edit-bundle-' . str_replace('_', '-', $bundle_name) . '-show-value-' . str_replace('_', '-', $field_name) => array('checked' => TRUE),
  227. ),
  228. );
  229. // All field widgets get reassigned weights so that additional elements
  230. // added between them (such as "_append") can be properly ordered.
  231. $form[$form_key][$field_name]['#weight'] = $weight++;
  232. $field_info = field_info_field($field_name);
  233. if ($field_info['cardinality'] != 1) {
  234. $form[$form_key]['_append::' . $field_name] = array(
  235. '#type' => 'checkbox',
  236. '#title' => t('Add new value(s) to %label, instead of overwriting the existing values.', array('%label' => $field['label'])),
  237. '#states' => array(
  238. 'visible' => array(
  239. '#edit-bundle-' . str_replace('_', '-', $bundle_name) . '-show-value-' . str_replace('_', '-', $field_name) => array('checked' => TRUE),
  240. ),
  241. ),
  242. '#weight' => $weight++,
  243. );
  244. }
  245. }
  246. }
  247. // Add a clearfix below the checkboxes so that the widgets are not floated.
  248. $form[$form_key]['show_value']['#suffix'] = '<div class="clearfix"></div>';
  249. $form[$form_key]['show_value']['#weight'] = -1;
  250. }
  251. // If the form has only one group (for example, "Properties"), remove the
  252. // title and the fieldset, since there's no need to visually group values.
  253. $form_elements = element_get_visible_children($form);
  254. if (count($form_elements) == 1) {
  255. $element_key = reset($form_elements);
  256. unset($form[$element_key]['#type']);
  257. unset($form[$element_key]['#title']);
  258. // Get a list of all elements in the group, and filter out the non-values.
  259. $values = element_get_visible_children($form[$element_key]);
  260. foreach ($values as $index => $key) {
  261. if ($key == 'show_value' || substr($key, 0, 1) == '_') {
  262. unset($values[$index]);
  263. }
  264. }
  265. // If the group has only one value, no need to hide it through #states.
  266. if (count($values) == 1) {
  267. $value_key = reset($values);
  268. $form[$element_key]['show_value'][$value_key]['#type'] = 'value';
  269. $form[$element_key]['show_value'][$value_key]['#value'] = TRUE;
  270. }
  271. }
  272. if (module_exists('token') && $context['settings']['show_all_tokens']) {
  273. $token_type = str_replace('_', '-', $entity_type);
  274. $form['tokens'] = array(
  275. '#type' => 'fieldset',
  276. '#title' => t('Available tokens'),
  277. '#collapsible' => TRUE,
  278. '#collapsed' => TRUE,
  279. '#weight' => 998,
  280. );
  281. $form['tokens']['tree'] = array(
  282. '#theme' => 'token_tree',
  283. '#token_types' => array($token_type, 'site'),
  284. '#global_types' => array(),
  285. '#dialog' => TRUE,
  286. );
  287. }
  288. return $form;
  289. }
  290. /**
  291. * Action form validate function.
  292. *
  293. * Checks that the user selected at least one value to modify, validates
  294. * properties and calls Field API to validate fields for each bundle.
  295. */
  296. function views_bulk_operations_modify_action_validate($form, &$form_state) {
  297. // The form structure for "Show" checkboxes is a bit bumpy.
  298. $search = array('properties');
  299. foreach ($form_state['entities'] as $bundle => $entity) {
  300. $search[] = 'bundle_' . $bundle;
  301. }
  302. $has_selected = FALSE;
  303. foreach ($search as $group) {
  304. // Store names of selected and appended entity values in a nicer format.
  305. $form_state['selected'][$group] = array();
  306. $form_state['append'][$group] = array();
  307. // This group has no values, move on.
  308. if (!isset($form_state['values'][$group])) {
  309. continue;
  310. }
  311. foreach ($form_state['values'][$group]['show_value'] as $key => $value) {
  312. if ($value) {
  313. $has_selected = TRUE;
  314. $form_state['selected'][$group][] = $key;
  315. }
  316. if (!empty($form_state['values'][$group]['_append::' . $key])) {
  317. $form_state['append'][$group][] = $key;
  318. unset($form_state['values'][$group]['_append::' . $key]);
  319. }
  320. }
  321. unset($form_state['values'][$group]['show_value']);
  322. }
  323. if (!$has_selected) {
  324. form_set_error('', t('You must select at least one value to modify.'));
  325. return;
  326. }
  327. // Use the wrapper to validate property values.
  328. if (!empty($form_state['selected']['properties'])) {
  329. // The entity used is irrelevant, and we can't rely on
  330. // $form_state['entities'] being non-empty, so a new one is created.
  331. $info = entity_get_info($form_state['entity_type']);
  332. $bundle_key = $info['entity keys']['bundle'];
  333. $default_values = array();
  334. // If the bundle key exists, it must always be set on an entity.
  335. if (!empty($bundle_key)) {
  336. $bundle_names = array_keys($info['bundles']);
  337. $bundle_name = reset($bundle_names);
  338. $default_values[$bundle_key] = $bundle_name;
  339. }
  340. $entity = entity_create($form_state['entity_type'], $default_values);
  341. $wrapper = entity_metadata_wrapper($form_state['entity_type'], $entity);
  342. $properties = _views_bulk_operations_modify_action_get_properties($form_state['entity_type']);
  343. foreach ($form_state['selected']['properties'] as $key) {
  344. $value = $form_state['values']['properties'][$key];
  345. if (!$wrapper->$key->validate($value)) {
  346. $label = $properties[$key]['label'];
  347. form_set_error('properties][' . $key, t('%label contains an invalid value.', array('%label' => $label)));
  348. }
  349. }
  350. }
  351. foreach ($form_state['entities'] as $bundle_name => $entity) {
  352. field_attach_form_validate($form_state['entity_type'], $entity, $form['bundle_' . $bundle_name], $form_state);
  353. }
  354. }
  355. /**
  356. * Action form submit function.
  357. *
  358. * Fills each constructed entity with property and field values, then
  359. * passes them to views_bulk_operations_modify_action().
  360. */
  361. function views_bulk_operations_modify_action_submit($form, $form_state) {
  362. foreach ($form_state['entities'] as $bundle_name => $entity) {
  363. field_attach_submit($form_state['entity_type'], $entity, $form['bundle_' . $bundle_name], $form_state);
  364. }
  365. return array(
  366. 'append' => $form_state['append'],
  367. 'selected' => $form_state['selected'],
  368. 'entities' => $form_state['entities'],
  369. 'properties' => isset($form_state['values']['properties']) ? $form_state['values']['properties'] : array(),
  370. );
  371. }
  372. /**
  373. * Returns all properties that can be modified.
  374. *
  375. * Properties that can't be changed are entity keys, timestamps, and the ones
  376. * without a setter callback.
  377. *
  378. * @param $entity_type
  379. * The entity type whose properties will be fetched.
  380. * @param $display_values
  381. * An optional, admin-provided list of properties and fields that should be
  382. * displayed for editing, used to filter the returned list of properties.
  383. */
  384. function _views_bulk_operations_modify_action_get_properties($entity_type, $display_values = NULL) {
  385. $properties = array();
  386. $info = entity_get_info($entity_type);
  387. // List of properties that can't be modified.
  388. $disabled_properties = array('created', 'changed');
  389. foreach (array('id', 'bundle', 'revision') as $key) {
  390. if (!empty($info['entity keys'][$key])) {
  391. $disabled_properties[] = $info['entity keys'][$key];
  392. }
  393. }
  394. // List of supported types.
  395. $supported_types = array('text', 'token', 'integer', 'decimal', 'date', 'duration',
  396. 'boolean', 'uri', 'list');
  397. $property_info = entity_get_property_info($entity_type);
  398. if (empty($property_info['properties'])) {
  399. // Stop here if no properties were found.
  400. return array();
  401. }
  402. foreach ($property_info['properties'] as $key => $property) {
  403. if (in_array($key, $disabled_properties)) {
  404. continue;
  405. }
  406. // Filter out properties that can't be set (they are usually generated by a
  407. // getter callback based on other properties, and not stored in the DB).
  408. if (empty($property['setter callback'])) {
  409. continue;
  410. }
  411. // Determine the property type. If it's empty (permitted), default to text.
  412. // If it's a list type such as list<boolean>, extract the "boolean" part.
  413. $property['type'] = empty($property['type']) ? 'text' : $property['type'];
  414. $type = $property['type'];
  415. if ($list_type = entity_property_list_extract_type($type)) {
  416. $type = $list_type;
  417. $property['type'] = 'list';
  418. }
  419. // Filter out non-supported types (such as the Field API fields that
  420. // Commerce adds to its entities so that they show up in tokens).
  421. if (!in_array($type, $supported_types)) {
  422. continue;
  423. }
  424. $properties[$key] = $property;
  425. }
  426. if (isset($display_values) && empty($display_values[VBO_MODIFY_ACTION_ALL])) {
  427. // Return only the properties that the admin specified.
  428. return array_intersect_key($properties, $display_values);
  429. }
  430. return $properties;
  431. }
  432. /**
  433. * Returns all bundles for which field widgets should be displayed.
  434. *
  435. * If the admin decided to limit the modify form to certain properties / fields
  436. * (through the action settings) then only bundles that have at least one field
  437. * selected are returned.
  438. *
  439. * @param $entity_type
  440. * The entity type whose bundles will be fetched.
  441. * @param $context
  442. * The VBO context variable.
  443. */
  444. function _views_bulk_operations_modify_action_get_bundles($entity_type, $context) {
  445. $bundles = array();
  446. $view = $context['view'];
  447. $vbo = _views_bulk_operations_get_field($view);
  448. $display_values = $context['settings']['display_values'];
  449. $info = entity_get_info($entity_type);
  450. $bundle_key = $info['entity keys']['bundle'];
  451. // Check if this View has a filter on the bundle key and assemble a list
  452. // of allowed bundles according to the filter.
  453. $filtered_bundles = array_keys($info['bundles']);
  454. // Go over all the filters and find any relevant ones.
  455. foreach ($view->filter as $key => $filter) {
  456. // Check it's the right field on the right table.
  457. if ($filter->table == $vbo->table && $filter->field == $bundle_key) {
  458. // Exposed filters may have no bundles, so check that there is a value.
  459. if (empty($filter->value)) {
  460. continue;
  461. }
  462. $operator = $filter->operator;
  463. if ($operator == 'in') {
  464. $filtered_bundles = array_intersect($filtered_bundles, $filter->value);
  465. }
  466. elseif ($operator == 'not in') {
  467. $filtered_bundles = array_diff($filtered_bundles, $filter->value);
  468. }
  469. }
  470. }
  471. foreach ($info['bundles'] as $bundle_name => $bundle) {
  472. // The view is limited to specific bundles, but this bundle isn't one of
  473. // them. Ignore it.
  474. if (!in_array($bundle_name, $filtered_bundles)) {
  475. continue;
  476. }
  477. $instances = field_info_instances($entity_type, $bundle_name);
  478. // Ignore bundles that don't have any field instances attached.
  479. if (empty($instances)) {
  480. continue;
  481. }
  482. $has_enabled_fields = FALSE;
  483. foreach ($display_values as $key) {
  484. if (strpos($key, $bundle_name . '::') !== FALSE) {
  485. $has_enabled_fields = TRUE;
  486. }
  487. }
  488. // The admin has either specified that all values should be modifiable, or
  489. // selected at least one field belonging to this bundle.
  490. if (!empty($display_values[VBO_MODIFY_ACTION_ALL]) || $has_enabled_fields) {
  491. $bundles[$bundle_name] = $bundle;
  492. }
  493. }
  494. return $bundles;
  495. }
  496. /**
  497. * Helper function that recursively strips #required from field widgets.
  498. */
  499. function _views_bulk_operations_modify_action_unset_required(&$element) {
  500. unset($element['#required']);
  501. foreach (element_children($element) as $key) {
  502. _views_bulk_operations_modify_action_unset_required($element[$key]);
  503. }
  504. }
  505. /**
  506. * VBO settings form function.
  507. */
  508. function views_bulk_operations_modify_action_views_bulk_operations_form_options() {
  509. $options['show_all_tokens'] = TRUE;
  510. $options['display_values'] = array(VBO_MODIFY_ACTION_ALL);
  511. return $options;
  512. }
  513. /**
  514. * The settings form for this action.
  515. */
  516. function views_bulk_operations_modify_action_views_bulk_operations_form($options, $entity_type, $dom_id) {
  517. // Initialize default values.
  518. if (empty($options)) {
  519. $options = views_bulk_operations_modify_action_views_bulk_operations_form_options();
  520. }
  521. $form['show_all_tokens'] = array(
  522. '#type' => 'checkbox',
  523. '#title' => t('Show available tokens'),
  524. '#description' => t('Check this to show a list of all available tokens in the bottom of the form. Requires the token module.'),
  525. '#default_value' => $options['show_all_tokens'],
  526. );
  527. $info = entity_get_info($entity_type);
  528. $properties = _views_bulk_operations_modify_action_get_properties($entity_type);
  529. $values = array(VBO_MODIFY_ACTION_ALL => t('- All -'));
  530. foreach ($properties as $key => $property) {
  531. $label = t('Properties');
  532. $values[$label][$key] = $property['label'];
  533. }
  534. foreach ($info['bundles'] as $bundle_name => $bundle) {
  535. $bundle_key = $info['entity keys']['bundle'];
  536. // Show the more detailed label only if the entity type has multiple bundles.
  537. // Otherwise, it would just be confusing.
  538. if (count($info['bundles']) > 1) {
  539. $label = t('Fields for @bundle_key @label', array('@bundle_key' => $bundle_key, '@label' => $bundle['label']));
  540. }
  541. else {
  542. $label = t('Fields');
  543. }
  544. $instances = field_info_instances($entity_type, $bundle_name);
  545. foreach ($instances as $field_name => $field) {
  546. $values[$label][$bundle_name . '::' . $field_name] = $field['label'];
  547. }
  548. }
  549. $form['display_values'] = array(
  550. '#type' => 'select',
  551. '#title' => t('Display values'),
  552. '#options' => $values,
  553. '#multiple' => TRUE,
  554. '#description' => t('Select which values the action form should present to the user.'),
  555. '#default_value' => $options['display_values'],
  556. '#size' => 10,
  557. );
  558. return $form;
  559. }