Projet

Général

Profil

Révision 7fe061e8

Ajouté par Assos Assos il y a plus de 8 ans

Weekly update of contrib modules

Voir les différences:

drupal7/sites/all/modules/ds/modules/ds_ui/includes/ds.fields.inc
114 114
}
115 115

  
116 116
/**
117
 * Shared form for all all fields.
117
 * Shared form for all fields.
118 118
 */
119 119
function ds_shared_form(&$form, $field) {
120 120
  ctools_include('export');
......
176 176
    '#description' => t('Limit this field on field UI per bundles and/or view modes. The values are in the form of $bundle|$view_mode, where $view_mode may be either a view mode set to use custom settings, or \'default\'. You may use * to select all, e.g article|*, *|full or *|*. Enter one value per line.'),
177 177
    '#type' => 'textarea',
178 178
    '#default_value' => $field->ui_limit,
179
    '#element_validate' => array('ds_ui_limit_validate'),
179 180
  );
180 181

  
181 182
  $form['submit'] = array(
......
191 192
  return $field;
192 193
}
193 194

  
195
/**
196
 * Element validation function for ui limit field.
197
 */
198
function ds_ui_limit_validate($element, &$form_state, $form) {
199
  // Get all enabled entity types.
200
  $entity_types = array_filter($form_state['values']['entities']);
201

  
202
  if (!empty($element['#value'])) {
203
    $ui_limit = $element['#value'];
204

  
205
    $lines = explode("\n", str_replace("\r", "\n", $ui_limit));
206
    // Ensure that all strings are trimmed and filter out empty lines.
207
    $lines = array_filter(array_map('trim', $lines));
208

  
209
    $error = FALSE;
210
    foreach ($lines as $line) {
211
      // Each line should hold a pipe character to seperate bundle and view_mode.
212
      if (strpos($line, '|') === FALSE) {
213
        $error = TRUE;
214
        continue;
215
      }
216

  
217
      list($bundle, $view_mode) = explode('|', $line);
218

  
219
      if (empty($bundle) || empty($view_mode) || !_ds_check_existing_ui_limit($entity_types, $bundle, $view_mode)) {
220
        $error = TRUE;
221
      }
222
    }
223
    if ($error) {
224
      form_error($element, t('The values are in the form of $bundle|$view_mode, where $view_mode may be either a view mode set to use custom settings, or \'default\'.'));
225
    }
226

  
227
    // Set trimmed and validated entry back as value.
228
    form_set_value($element, implode("\n", $lines), $form_state);
229
  }
230
}
231

  
232
/**
233
 * Helper function to check if bundle + view_mode combination exists.
234
 */
235
function _ds_check_existing_ui_limit($entity_types, $bundle, $view_mode) {
236
  $exists = FALSE;
237
  foreach ($entity_types as $entity_type) {
238
    $info = entity_get_info($entity_type);
239

  
240
    // Combine allowed bundles and entity specific ones.
241
    $bundle_allowed = array('*');
242
    $bundles = array_merge($bundle_allowed, array_keys($info['bundles']));
243

  
244
    // Combine allowed view_modes and entity specific ones.
245
    $view_mode_allowed = array('*', 'default');
246
    $view_modes = array_merge($view_mode_allowed, array_keys($info['view modes']));
247

  
248
    if (in_array($bundle, $bundles) &&
249
      in_array($view_mode, $view_modes)) {
250

  
251
      $exists = TRUE;
252
      break;
253
    }
254
  }
255
  if (!$exists) {
256
    drupal_set_message(t('Incorrect field limit combination: @bundle|@view_mode', array('@bundle' => $bundle, '@view_mode' => $view_mode)), 'error');
257
  }
258

  
259
  return $exists;
260
}
261

  
194 262
/**
195 263
 * Return whether a field machine name is unique.
196 264
 */
......
209 277
  $field->properties = array();
210 278
  $field->field = $form_state['values']['field'];
211 279
  $field->label = $form_state['values']['name'];
212
  $field->ui_limit = str_replace("\r", '', $form_state['values']['ui_limit']);
280
  $field->ui_limit = $form_state['values']['ui_limit'];
213 281

  
214 282
  $entities = $form_state['values']['entities'];
215 283
  foreach ($entities as $key => $value) {

Formats disponibles : Unified diff