Projet

Général

Profil

Révision 7547bb19

Ajouté par Assos Assos il y a environ 7 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/views_bulk_operations/actions/modify.action.inc
1 1
<?php
2 2

  
3 3
/**
4
 * @file VBO action to modify entity values (properties and fields).
4
 * @file
5
 * VBO action to modify entity values (properties and fields).
5 6
 */
6 7

  
7 8
// Specifies that all available values should be shown to the user for editing.
8 9
define('VBO_MODIFY_ACTION_ALL', '_all_');
9 10

  
11
/**
12
 * Implements hook_action_info().
13
 */
10 14
function views_bulk_operations_modify_action_info() {
11
  return array('views_bulk_operations_modify_action' => array(
12
    'type' => 'entity',
13
    'label' => t('Modify entity values'),
14
    'behavior' => array('changes_property'),
15
    // This action only works when invoked through VBO. That's why it's
16
    // declared as non-configurable to prevent it from being shown in the
17
    // "Create an advanced action" dropdown on admin/config/system/actions.
18
    'configurable' => FALSE,
19
    'vbo_configurable' => TRUE,
20
    'triggers' => array('any'),
21
  ));
15
  return array(
16
    'views_bulk_operations_modify_action' => array(
17
      'type' => 'entity',
18
      'label' => t('Modify entity values'),
19
      'behavior' => array('changes_property'),
20
      // This action only works when invoked through VBO. That's why it's
21
      // declared as non-configurable to prevent it from being shown in the
22
      // "Create an advanced action" dropdown on admin/config/system/actions.
23
      'configurable' => FALSE,
24
      'vbo_configurable' => TRUE,
25
      'triggers' => array('any'),
26
    ),
27
  );
22 28
}
23 29

  
24 30
/**
......
28 34
 * replacing the existing values, or appending to them (based on user input).
29 35
 */
30 36
function views_bulk_operations_modify_action($entity, $context) {
31
  list(,,$bundle_name) = entity_extract_ids($context['entity_type'], $entity);
37
  list(,, $bundle_name) = entity_extract_ids($context['entity_type'], $entity);
32 38
  // Handle Field API fields.
33 39
  if (!empty($context['selected']['bundle_' . $bundle_name])) {
34 40
    // The pseudo entity is cloned so that changes to it don't get carried
......
38 44
      // Get this field's language. We can just pull it from the pseudo entity
39 45
      // as it was created using field_attach_form and entity_language so it's
40 46
      // already been figured out if this field is translatable or not and
41
      // applied the appropriate language code to the field
47
      // applied the appropriate language code to the field.
42 48
      $language = key($pseudo_entity->{$key});
43 49
      // Replace any tokens that might exist in the field columns.
44 50
      foreach ($pseudo_entity->{$key}[$language] as $delta => &$item) {
......
58 64
        if ($field_info['cardinality'] != FIELD_CARDINALITY_UNLIMITED && $field_count > $field_info['cardinality']) {
59 65
          $entity_label = entity_label($context['entity_type'], $entity);
60 66
          $warning = t('Tried to set !field_count values for field !field_name that supports a maximum of !cardinality.',
61
                       array('!field_count' => $field_count,
62
                             '!field_name' => $field_info['field_name'],
63
                             '!cardinality' => $field_info['cardinality']));
67
            array(
68
              '!field_count' => $field_count,
69
              '!field_name' => $field_info['field_name'],
70
              '!cardinality' => $field_info['cardinality'],
71
            ));
64 72
          drupal_set_message($warning, 'warning', FALSE);
65 73
        }
66 74

  
......
76 84
  }
77 85

  
78 86
  // Handle properties.
87
  // Use the wrapper to set property values, since some properties need
88
  // additional massaging by their setter callbacks.
89
  // The wrapper will automatically modify $entity itself.
90
  $wrapper = entity_metadata_wrapper($context['entity_type'], $entity);
91
  // The default setting for 'revision' property (create new revisions) should
92
  // be respected for nodes. This requires some special treatment.
93
  if ($context['entity_type'] == 'node' && in_array('revision', variable_get('node_options_' . $bundle_name)) && !in_array('revision', $context['selected']['properties'])) {
94
    $wrapper->revision->set(1);
95
  }
96

  
79 97
  if (!empty($context['selected']['properties'])) {
80
    // Use the wrapper to set property values, since some properties need
81
    // additional massaging by their setter callbacks.
82
    // The wrapper will automatically modify $entity itself.
83
    $wrapper = entity_metadata_wrapper($context['entity_type'], $entity);
84 98
    foreach ($context['selected']['properties'] as $key) {
85 99
      if (!$wrapper->$key->access('update')) {
86 100
        // No access.
......
113 127
 * entity bundle, as provided by field_attach_form().
114 128
 */
115 129
function views_bulk_operations_modify_action_form($context, &$form_state) {
116
  // This action form uses admin-provided settings. If they were not set, pull the defaults now.
130
  // This action form uses admin-provided settings. If they were not set, pull
131
  // the defaults now.
117 132
  if (!isset($context['settings'])) {
118 133
    $context['settings'] = views_bulk_operations_modify_action_views_bulk_operations_form_options();
119 134
  }
......
126 141
  // and filled with form data.
127 142
  // After submit, the pseudo-entities get passed to the actual action
128 143
  // (views_bulk_operations_modify_action()) which copies the data from the
129
  // relevant pseudo-entity constructed here to the actual entity being modified.
144
  // relevant pseudo-entity constructed here to the actual entity being
145
  // modified.
130 146
  $form_state['entities'] = array();
131 147

  
132 148
  $info = entity_get_info($entity_type);
......
151 167
        '#title' => $property['label'],
152 168
      );
153 169

  
154
      $determined_type = ($property['type'] == 'boolean') ? 'checkbox' : 'textfield';
170
      // According to _views_bulk_operations_modify_action_get_properties
171
      // we have fixed list of supported types. Most of these types are string
172
      // and only some of them has options list.
173
      if (isset($property['options list'])) {
174
        $determined_type = ($property['type'] == 'list') ? 'checkboxes' : 'select';
175
      }
176
      else {
177
        $determined_type = ($property['type'] == 'boolean') ? 'checkbox' : 'textfield';
178
      }
179

  
155 180
      $form['properties'][$key] = array(
156 181
        '#type' => $determined_type,
157 182
        '#title' => $property['label'],
......
189 214
    }
190 215
  }
191 216

  
192
  // Going to need this for multilingual nodes
217
  // Going to need this for multilingual nodes.
193 218
  global $language;
194 219
  foreach ($bundles as $bundle_name => $bundle) {
195 220
    $bundle_key = $info['entity keys']['bundle'];
......
202 227
    $entity = entity_create($context['entity_type'], $default_values);
203 228
    $form_state['entities'][$bundle_name] = $entity;
204 229

  
205
    // Show the more detailed label only if the entity type has multiple bundles.
206
    // Otherwise, it would just be confusing.
230
    // Show the more detailed label only if the entity type has multiple
231
    // bundles. Otherwise, it would just be confusing.
207 232
    if (count($info['bundles']) > 1) {
208 233
      $label = t('Fields for @bundle_key @label', array('@bundle_key' => $bundle_key, '@label' => $bundle['label']));
209 234
    }
......
417 442
 * Properties that can't be changed are entity keys, timestamps, and the ones
418 443
 * without a setter callback.
419 444
 *
420
 * @param $entity_type
445
 * @param string $entity_type
421 446
 *   The entity type whose properties will be fetched.
422
 * @param $display_values
447
 * @param array $display_values
423 448
 *   An optional, admin-provided list of properties and fields that should be
424 449
 *   displayed for editing, used to filter the returned list of properties.
425 450
 */
......
435 460
    }
436 461
  }
437 462
  // List of supported types.
438
  $supported_types = array('text', 'token', 'integer', 'decimal', 'date', 'duration',
439
                           'boolean', 'uri', 'list');
463
  $supported_types = array(
464
    'text',
465
    'token',
466
    'integer',
467
    'decimal',
468
    'date',
469
    'duration',
470
    'boolean',
471
    'uri',
472
    'list',
473
  );
440 474
  $property_info = entity_get_property_info($entity_type);
441 475
  if (empty($property_info['properties'])) {
442 476
    // Stop here if no properties were found.
......
484 518
 * (through the action settings) then only bundles that have at least one field
485 519
 * selected are returned.
486 520
 *
487
 * @param $entity_type
521
 * @param string $entity_type
488 522
 *   The entity type whose bundles will be fetched.
489
 * @param $context
523
 * @param array $context
490 524
 *   The VBO context variable.
491 525
 */
492 526
function _views_bulk_operations_modify_action_get_bundles($entity_type, $context) {
......
594 628
  }
595 629
  foreach ($info['bundles'] as $bundle_name => $bundle) {
596 630
    $bundle_key = $info['entity keys']['bundle'];
597
    // Show the more detailed label only if the entity type has multiple bundles.
598
    // Otherwise, it would just be confusing.
631
    // Show the more detailed label only if the entity type has multiple
632
    // bundles. Otherwise, it would just be confusing.
599 633
    if (count($info['bundles']) > 1) {
600 634
      $label = t('Fields for @bundle_key @label', array('@bundle_key' => $bundle_key, '@label' => $bundle['label']));
601 635
    }

Formats disponibles : Unified diff