Project

General

Profile

Paste
Download (21.1 KB) Statistics
| Branch: | Revision:

root / drupal7 / modules / field_ui / field_ui.module @ b0dc3a2e

1
<?php
2
/**
3
 * @file
4
 * Allows administrators to attach custom fields to fieldable types.
5
 */
6

    
7
/**
8
 * Implements hook_help().
9
 */
10
function field_ui_help($path, $arg) {
11
  switch ($path) {
12
    case 'admin/help#field_ui':
13
      $output = '';
14
      $output .= '<h3>' . t('About') . '</h3>';
15
      $output .= '<p>' . t('The Field UI module provides an administrative user interface (UI) for attaching and managing fields. Fields can be defined at the content-type level for content items and comments, at the vocabulary level for taxonomy terms, and at the site level for user accounts. Other modules may also enable fields to be defined for their data. Field types (text, image, number, etc.) are defined by modules, and collected and managed by the <a href="@field">Field module</a>. For more information, see the online handbook entry for <a href="@field_ui" target="_blank">Field UI module</a>.', array('@field' => url('admin/help/field'), '@field_ui' => 'http://drupal.org/documentation/modules/field-ui')) . '</p>';
16
      $output .= '<h3>' . t('Uses') . '</h3>';
17
      $output .= '<dl>';
18
      $output .= '<dt>' . t('Planning fields') . '</dt>';
19
      $output .= '<dd>' . t('There are several decisions you will need to make before defining a field for content, comments, etc.:') . '<dl>';
20
      $output .= '<dt>' . t('What the field will be called') . '</dt>';
21
      $output .= '<dd>' . t('A field has a <em>label</em> (the name displayed in the user interface) and a <em>machine name</em> (the name used internally). The label can be changed after you create the field, if needed, but the machine name cannot be changed after you have created the field.') . '</li>';
22
      $output .= '<dt>' . t('What type of data the field will store') . '</dt>';
23
      $output .= '<dd>' . t('Each field can store one type of data (text, number, file, etc.). When you define a field, you choose a particular <em>field type</em>, which corresponds to the type of data you want to store. The field type cannot be changed after you have created the field.') . '</dd>';
24
      $output .= '<dt>' . t('How the data will be input and displayed') . '</dt>';
25
      $output .= '<dd>' . t('Each field type has one or more available <em>widgets</em> associated with it; each widget provides a mechanism for data input when you are editing (text box, select list, file upload, etc.). Each field type also has one or more display options, which determine how the field is displayed to site visitors. The widget and display options can be changed after you have created the field.') . '</dd>';
26
      $output .= '<dt>' . t('How many values the field will store') . '</dt>';
27
      $output .= '<dd>' . t('You can store one value, a specific maximum number of values, or an unlimited number of values in each field. For example, an employee identification number field might store a single number, whereas a phone number field might store multiple phone numbers. This setting can be changed after you have created the field, but if you reduce the maximum number of values, you may lose information.') . '</dd>';
28
      $output .= '</dl>';
29
      $output .= '<dt>' . t('Reusing fields') . '</dt>';
30
      $output .= '<dd>' . t('Once you have defined a field, you can reuse it. For example, if you define a custom image field for one content type, and you need to have an image field with the same parameters on another content type, you can add the same field to the second content type, in the <em>Add existing field</em> area of the user interface. You could also add this field to a taxonomy vocabulary, comments, user accounts, etc.') . '</dd>';
31
      $output .= '<dd>' . t('Some settings of a reused field are unique to each use of the field; others are shared across all places you use the field. For example, the label of a text field is unique to each use, while the setting for the number of values is shared.') . '</dd>';
32
      $output .= '<dd>' . t('There are two main reasons for reusing fields. First, reusing fields can save you time over defining new fields. Second, reusing fields also allows you to display, filter, group, and sort content together by field across content types. For example, the contributed Views module allows you to create lists and tables of content. So if you use the same field on multiple content types, you can create a View containing all of those content types together displaying that field, sorted by that field, and/or filtered by that field.') . '</dd>';
33
      $output .= '<dt>' . t('Fields on content items') . '</dt>';
34
      $output .= '<dd>' . t('Fields on content items are defined at the content-type level, on the <em>Manage fields</em> tab of the content type edit page (which you can reach from the <a href="@types">Content types page</a>). When you define a field for a content type, each content item of that type will have that field added to it. Some fields, such as the Title and Body, are provided for you when you create a content type, or are provided on content types created by your installation profile.', array('@types' => url('admin/structure/types'))) . '</dd>';
35
      $output .= '<dt>' . t('Fields on taxonomy terms') . '</dt>';
36
      $output .= '<dd>' . t('Fields on taxonomy terms are defined at the taxonomy vocabulary level, on the <em>Manage fields</em> tab of the vocabulary edit page (which you can reach from the <a href="@taxonomy">Taxonomy page</a>). When you define a field for a vocabulary, each term in that vocabulary will have that field added to it. For example, you could define an image field for a vocabulary to store an icon with each term.', array('@taxonomy' => url('admin/structure/taxonomy'))) . '</dd>';
37
      $output .= '<dt>' . t('Fields on user accounts') . '</dt>';
38
      $output .= '<dd>' . t('Fields on user accounts are defined on a site-wide basis on the <a href="@fields">Manage fields tab</a> of the <a href="@accounts">Account settings</a> page. When you define a field for user accounts, each user account will have that field added to it. For example, you could add a long text field to allow users to include a biography.', array('@fields' => url('admin/config/people/accounts/fields'), '@accounts' => url('admin/config/people/accounts'))) . '</dd>';
39
      $output .= '<dt>' . t('Fields on comments') . '</dt>';
40
      $output .= '<dd>' . t('Fields on comments are defined at the content-type level, on the <em>Comment fields</em> tab of the content type edit page (which you can reach from the <a href="@types">Content types page</a>). When you add a field for comments, each comment on a content item of that type will have that field added to it. For example, you could add a website field to the comments on forum posts, to allow forum commenters to add a link to their website.', array('@types' => url('admin/structure/types'))) . '</dd>';
41
      $output .= '</dl>';
42
      return $output;
43

    
44
    case 'admin/reports/fields':
45
      return '<p>' . t('This list shows all fields currently in use for easy reference.') . '</p>';
46
  }
47
}
48

    
49
/**
50
 * Implements hook_field_attach_rename_bundle().
51
 */
52
function field_ui_field_attach_rename_bundle($entity_type, $bundle_old, $bundle_new) {
53
  // The Field UI relies on entity_get_info() to build menu items for entity
54
  // field administration pages. Clear the entity info cache and ensure that
55
  // the menu is rebuilt.
56
  entity_info_cache_clear();
57
  menu_rebuild();
58
}
59

    
60
/**
61
 * Implements hook_menu().
62
 */
63
function field_ui_menu() {
64
  $items['admin/reports/fields'] = array(
65
    'title' => 'Field list',
66
    'description' => 'Overview of fields on all entity types.',
67
    'page callback' => 'field_ui_fields_list',
68
    'access arguments' => array('administer content types'),
69
    'type' => MENU_NORMAL_ITEM,
70
    'file' => 'field_ui.admin.inc',
71
  );
72

    
73
  // Ensure the following is not executed until field_bundles is working and
74
  // tables are updated. Needed to avoid errors on initial installation.
75
  if (defined('MAINTENANCE_MODE')) {
76
    return $items;
77
  }
78

    
79
  // Create tabs for all possible bundles.
80
  foreach (entity_get_info() as $entity_type => $entity_info) {
81
    if ($entity_info['fieldable']) {
82
      foreach ($entity_info['bundles'] as $bundle_name => $bundle_info) {
83
        if (isset($bundle_info['admin'])) {
84
          // Extract path information from the bundle.
85
          $path = $bundle_info['admin']['path'];
86
          // Different bundles can appear on the same path (e.g. %node_type and
87
          // %comment_node_type). To allow field_ui_menu_load() to extract the
88
          // actual bundle object from the translated menu router path
89
          // arguments, we need to identify the argument position of the bundle
90
          // name string ('bundle argument') and pass that position to the menu
91
          // loader. The position needs to be casted into a string; otherwise it
92
          // would be replaced with the bundle name string.
93
          if (isset($bundle_info['admin']['bundle argument'])) {
94
            $bundle_arg = $bundle_info['admin']['bundle argument'];
95
            $bundle_pos = (string) $bundle_arg;
96
          }
97
          else {
98
            $bundle_arg = $bundle_name;
99
            $bundle_pos = '0';
100
          }
101
          // This is the position of the %field_ui_menu placeholder in the
102
          // items below.
103
          $field_position = count(explode('/', $path)) + 1;
104

    
105
          // Extract access information, providing defaults.
106
          $access = array_intersect_key($bundle_info['admin'], drupal_map_assoc(array('access callback', 'access arguments')));
107
          $access += array(
108
            'access callback' => 'user_access',
109
            'access arguments' => array('administer fields'),
110
          );
111

    
112
          // Add the "administer fields" permission on top of the access
113
          // restriction because the field UI should only be accessible to
114
          // trusted users.
115
          if ($access['access callback'] != 'user_access' || $access['access arguments'] != array('administer fields')) {
116
            $access = array(
117
              'access callback' => 'field_ui_admin_access',
118
              'access arguments' => array($access['access callback'], $access['access arguments']),
119
            );
120
          }
121

    
122
          $items["$path/fields"] = array(
123
            'title' => 'Manage fields',
124
            'page callback' => 'drupal_get_form',
125
            'page arguments' => array('field_ui_field_overview_form', $entity_type, $bundle_arg),
126
            'type' => MENU_LOCAL_TASK,
127
            'weight' => 1,
128
            'file' => 'field_ui.admin.inc',
129
          ) + $access;
130
          $items["$path/fields/%field_ui_menu"] = array(
131
            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
132
            'title callback' => 'field_ui_menu_title',
133
            'title arguments' => array($field_position),
134
            'page callback' => 'drupal_get_form',
135
            'page arguments' => array('field_ui_field_edit_form', $field_position),
136
            'file' => 'field_ui.admin.inc',
137
          ) + $access;
138
          $items["$path/fields/%field_ui_menu/edit"] = array(
139
            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
140
            'title' => 'Edit',
141
            'page callback' => 'drupal_get_form',
142
            'page arguments' => array('field_ui_field_edit_form', $field_position),
143
            'type' => MENU_DEFAULT_LOCAL_TASK,
144
            'file' => 'field_ui.admin.inc',
145
          ) + $access;
146
          $items["$path/fields/%field_ui_menu/field-settings"] = array(
147
            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
148
            'title' => 'Field settings',
149
            'page callback' => 'drupal_get_form',
150
            'page arguments' => array('field_ui_field_settings_form', $field_position),
151
            'type' => MENU_LOCAL_TASK,
152
            'file' => 'field_ui.admin.inc',
153
          ) + $access;
154
          $items["$path/fields/%field_ui_menu/widget-type"] = array(
155
            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
156
            'title' => 'Widget type',
157
            'page callback' => 'drupal_get_form',
158
            'page arguments' => array('field_ui_widget_type_form', $field_position),
159
            'type' => MENU_LOCAL_TASK,
160
            'file' => 'field_ui.admin.inc',
161
          ) + $access;
162
          $items["$path/fields/%field_ui_menu/delete"] = array(
163
            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
164
            'title' => 'Delete',
165
            'page callback' => 'drupal_get_form',
166
            'page arguments' => array('field_ui_field_delete_form', $field_position),
167
            'type' => MENU_LOCAL_TASK,
168
            'weight' => 10,
169
            'file' => 'field_ui.admin.inc',
170
          ) + $access;
171

    
172
          // 'Manage display' tab.
173
          $items["$path/display"] = array(
174
            'title' => 'Manage display',
175
            'page callback' => 'drupal_get_form',
176
            'page arguments' => array('field_ui_display_overview_form', $entity_type, $bundle_arg, 'default'),
177
            'type' => MENU_LOCAL_TASK,
178
            'weight' => 2,
179
            'file' => 'field_ui.admin.inc',
180
          ) + $access;
181

    
182
          // View modes secondary tabs.
183
          // The same base $path for the menu item (with a placeholder) can be
184
          // used for all bundles of a given entity type; but depending on
185
          // administrator settings, each bundle has a different set of view
186
          // modes available for customisation. So we define menu items for all
187
          // view modes, and use an access callback to determine which ones are
188
          // actually visible for a given bundle.
189
          $weight = 0;
190
          $view_modes = array('default' => array('label' => t('Default'))) + $entity_info['view modes'];
191
          foreach ($view_modes as $view_mode => $view_mode_info) {
192
            $items["$path/display/$view_mode"] = array(
193
              'title' => $view_mode_info['label'],
194
              'page arguments' => array('field_ui_display_overview_form', $entity_type, $bundle_arg, $view_mode),
195
              // The access callback needs to check both the current 'custom
196
              // display' setting for the view mode, and the overall access
197
              // rules for the bundle admin pages.
198
              'access callback' => '_field_ui_view_mode_menu_access',
199
              'access arguments' => array_merge(array($entity_type, $bundle_arg, $view_mode, $access['access callback']), $access['access arguments']),
200
              'type' => ($view_mode == 'default' ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK),
201
              'weight' => ($view_mode == 'default' ? -10 : $weight++),
202
              'file' => 'field_ui.admin.inc',
203
            );
204
          }
205
        }
206
      }
207
    }
208
  }
209
  return $items;
210
}
211

    
212
/**
213
 * Menu loader; Load a field instance based on field and bundle name.
214
 *
215
 * @param $field_name
216
 *   The name of the field, as contained in the path.
217
 * @param $entity_type
218
 *   The name of the entity.
219
 * @param $bundle_name
220
 *   The name of the bundle, as contained in the path.
221
 * @param $bundle_pos
222
 *   The position of $bundle_name in $map.
223
 * @param $map
224
 *   The translated menu router path argument map.
225
 *
226
 * @return
227
 *   The field instance array.
228
 *
229
 * @ingroup field
230
 */
231
function field_ui_menu_load($field_name, $entity_type, $bundle_name, $bundle_pos, $map) {
232
  // Extract the actual bundle name from the translated argument map.
233
  // The menu router path to manage fields of an entity can be shared among
234
  // multiple bundles. For example:
235
  // - admin/structure/types/manage/%node_type/fields/%field_ui_menu
236
  // - admin/structure/types/manage/%comment_node_type/fields/%field_ui_menu
237
  // The menu system will automatically load the correct bundle depending on the
238
  // actual path arguments, but this menu loader function only receives the node
239
  // type string as $bundle_name, which is not the bundle name for comments.
240
  // We therefore leverage the dynamically translated $map provided by the menu
241
  // system to retrieve the actual bundle and bundle name for the current path.
242
  if ($bundle_pos > 0) {
243
    $bundle = $map[$bundle_pos];
244
    $bundle_name = field_extract_bundle($entity_type, $bundle);
245
  }
246
  // Check whether the field exists at all.
247
  if ($field = field_info_field($field_name)) {
248
    // Only return the field if a field instance exists for the given entity
249
    // type and bundle.
250
    if ($instance = field_info_instance($entity_type, $field_name, $bundle_name)) {
251
      return $instance;
252
    }
253
  }
254
  return FALSE;
255
}
256

    
257
/**
258
 * Menu title callback.
259
 */
260
function field_ui_menu_title($instance) {
261
  return $instance['label'];
262
}
263

    
264
/**
265
 * Menu access callback for the 'view mode display settings' pages.
266
 */
267
function _field_ui_view_mode_menu_access($entity_type, $bundle, $view_mode, $access_callback) {
268
  // First, determine visibility according to the 'use custom display'
269
  // setting for the view mode.
270
  $bundle = field_extract_bundle($entity_type, $bundle);
271
  $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
272
  $visibility = ($view_mode == 'default') || !empty($view_mode_settings[$view_mode]['custom_settings']);
273

    
274
  // Then, determine access according to the $access parameter. This duplicates
275
  // part of _menu_check_access().
276
  if ($visibility) {
277
    // Grab the variable 'access arguments' part.
278
    $all_args = func_get_args();
279
    $args = array_slice($all_args, 4);
280
    $callback = empty($access_callback) ? 0 : trim($access_callback);
281
    if (is_numeric($callback)) {
282
      return (bool) $callback;
283
    }
284
    else {
285
      // As call_user_func_array() is quite slow and user_access is a very
286
      // common callback, it is worth making a special case for it.
287
      if ($access_callback == 'user_access') {
288
        return (count($args) == 1) ? user_access($args[0]) : user_access($args[0], $args[1]);
289
      }
290
      elseif (function_exists($access_callback)) {
291
        return call_user_func_array($access_callback, $args);
292
      }
293
    }
294
  }
295
}
296

    
297
/**
298
 * Implements hook_theme().
299
 */
300
function field_ui_theme() {
301
  return array(
302
    'field_ui_table' => array(
303
      'render element' => 'elements',
304
    ),
305
  );
306
}
307

    
308
/**
309
 * Implements hook_element_info().
310
 */
311
function field_ui_element_info() {
312
  return array(
313
    'field_ui_table' => array(
314
      '#theme' => 'field_ui_table',
315
      '#pre_render' => array('field_ui_table_pre_render'),
316
      '#regions' => array('' => array()),
317
    ),
318
  );
319
}
320

    
321
/**
322
 * Implements hook_field_attach_create_bundle().
323
 */
324
function field_ui_field_attach_create_bundle($entity_type, $bundle) {
325
  // When a new bundle is created, the menu needs to be rebuilt to add our
326
  // menu item tabs.
327
  variable_set('menu_rebuild_needed', TRUE);
328
}
329

    
330
/**
331
 * Determines the administration path for a bundle.
332
 */
333
function _field_ui_bundle_admin_path($entity_type, $bundle_name) {
334
  $bundles = field_info_bundles($entity_type);
335
  $bundle_info = $bundles[$bundle_name];
336
  if (isset($bundle_info['admin'])) {
337
    return isset($bundle_info['admin']['real path']) ? $bundle_info['admin']['real path'] : $bundle_info['admin']['path'];
338
  }
339
}
340

    
341
/**
342
 * Identifies inactive fields within a bundle.
343
 */
344
function field_ui_inactive_instances($entity_type, $bundle_name = NULL) {
345
  $params = array('entity_type' => $entity_type);
346

    
347
  if (empty($bundle_name)) {
348
    $active = field_info_instances($entity_type);
349
    $inactive = array();
350
  }
351
  else {
352
    // Restrict to the specified bundle. For consistency with the case where
353
    // $bundle_name is NULL, the $active and  $inactive arrays are keyed by
354
    // bundle name first.
355
    $params['bundle'] = $bundle_name;
356
    $active = array($bundle_name => field_info_instances($entity_type, $bundle_name));
357
    $inactive = array($bundle_name => array());
358
  }
359

    
360
  // Iterate on existing definitions, and spot those that do not appear in the
361
  // $active list collected earlier.
362
  $all_instances = field_read_instances($params, array('include_inactive' => TRUE));
363
  foreach ($all_instances as $instance) {
364
    if (!isset($active[$instance['bundle']][$instance['field_name']])) {
365
      $inactive[$instance['bundle']][$instance['field_name']] = $instance;
366
    }
367
  }
368

    
369
  if (!empty($bundle_name)) {
370
    return $inactive[$bundle_name];
371
  }
372
  return $inactive;
373
}
374

    
375
/**
376
 * Implements hook_form_FORM_ID_alter().
377
 *
378
 * Adds a button 'Save and add fields' to the 'Create content type' form.
379
 *
380
 * @see node_type_form()
381
 * @see field_ui_form_node_type_form_submit()
382
 */
383
function field_ui_form_node_type_form_alter(&$form, $form_state) {
384
  // We want to display the button only on add page.
385
  if (empty($form['#node_type']->type)) {
386
    $form['actions']['save_continue'] = array(
387
      '#type' => 'submit',
388
      '#value' => t('Save and add fields'),
389
      '#weight' => 45,
390
    );
391
    $form['#submit'][] = 'field_ui_form_node_type_form_submit';
392
  }
393
}
394

    
395
/**
396
 * Form submission handler for the 'Save and add fields' button.
397
 *
398
 * @see field_ui_form_node_type_form_alter()
399
 */
400
function field_ui_form_node_type_form_submit($form, &$form_state) {
401
  if ($form_state['triggering_element']['#parents'][0] === 'save_continue') {
402
    $form_state['redirect'] = _field_ui_bundle_admin_path('node', $form_state['values']['type']) .'/fields';
403
  }
404
}
405

    
406
/**
407
 * Access callback to determine if a user is allowed to use the field UI.
408
 *
409
 * Only grant access if the user has both the "administer fields" permission and
410
 * is granted access by the entity specific restrictions.
411
 */
412
function field_ui_admin_access($access_callback, $access_arguments) {
413
  return user_access('administer fields') && call_user_func_array($access_callback, $access_arguments);
414
}