field_ui.module 20.9 KB
Newer Older
1 2 3
<?php
/**
 * @file
4
 * Allows administrators to attach custom fields to fieldable types.
5 6 7
 */

/**
8
 * Implements hook_help().
9 10 11 12
 */
function field_ui_help($path, $arg) {
  switch ($path) {
    case 'admin/help#field_ui':
13 14
      $output = '';
      $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 17
      $output .= '<h3>' . t('Uses') . '</h3>';
      $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 21 22 23 24
      $output .= '<dt>' . t('What the field will be called') . '</dt>';
      $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>';
      $output .= '<dt>' . t('What type of data the field will store') . '</dt>';
      $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>';
      $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 27 28 29
      $output .= '<dt>' . t('How many values the field will store') . '</dt>';
      $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>';
      $output .= '</dl>';
      $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>Re-use existing field</em> area of the user interface. You could also add this field to a taxonomy vocabulary, comments, user accounts, etc.') . '</dd>';
31 32 33 34 35 36 37 38 39 40
      $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>';
      $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>';
      $output .= '<dt>' . t('Fields on content items') . '</dt>';
      $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>';
      $output .= '<dt>' . t('Fields on taxonomy terms') . '</dt>';
      $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>';
      $output .= '<dt>' . t('Fields on user accounts') . '</dt>';
      $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>';
      $output .= '<dt>' . t('Fields on comments') . '</dt>';
      $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 43 44 45 46 47 48
      return $output;

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

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

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

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

97 98 99
          // User access check to be done against the permission to edit
          // fields or the display per entity type.
          $access_fields = array(
100
            'access callback' => 'user_access',
101 102 103 104 105
            'access arguments' => array('administer ' . $entity_type . ' fields'),
          );
          $access_display = array(
            'access callback' => 'user_access',
            'access arguments' => array('administer ' . $entity_type . ' display'),
106
          );
107

108 109
          $items["$path/fields"] = array(
            'title' => 'Manage fields',
110 111
            'page callback' => 'field_ui_field_overview',
            'page arguments' => array($entity_type, $bundle_arg),
112 113 114
            'type' => MENU_LOCAL_TASK,
            'weight' => 1,
            'file' => 'field_ui.admin.inc',
115
          ) + $access_fields;
116
          $items["$path/fields/%field_ui_instance"] = array(
117
            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
118
            'title callback' => 'field_ui_instance_title',
119
            'title arguments' => array($field_position),
120
            'page callback' => 'drupal_get_form',
121
            'page arguments' => array('field_ui_field_edit_form', $field_position),
122
            'file' => 'field_ui.admin.inc',
123
          ) + $access_fields;
124
          $items["$path/fields/%field_ui_instance/edit"] = array(
125 126
            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
            'title' => 'Edit',
127
            'page callback' => 'drupal_get_form',
128
            'page arguments' => array('field_ui_field_edit_form', $field_position),
129 130
            'type' => MENU_DEFAULT_LOCAL_TASK,
            'file' => 'field_ui.admin.inc',
131
          ) + $access_fields;
132
          $items["$path/fields/%field_ui_instance/field-settings"] = array(
133 134
            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
            'title' => 'Field settings',
135
            'page callback' => 'drupal_get_form',
136
            'page arguments' => array('field_ui_field_settings_form', $field_position),
137 138
            'type' => MENU_LOCAL_TASK,
            'file' => 'field_ui.admin.inc',
139
          ) + $access_fields;
140
          $items["$path/fields/%field_ui_instance/widget-type"] = array(
141 142
            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
            'title' => 'Widget type',
143
            'page callback' => 'drupal_get_form',
144
            'page arguments' => array('field_ui_widget_type_form', $field_position),
145 146
            'type' => MENU_LOCAL_TASK,
            'file' => 'field_ui.admin.inc',
147
          ) + $access_fields;
148
          $items["$path/fields/%field_ui_instance/delete"] = array(
149 150
            'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
            'title' => 'Delete',
151
            'page callback' => 'drupal_get_form',
152
            'page arguments' => array('field_ui_field_delete_form', $field_position),
153
            'type' => MENU_VISIBLE_IN_BREADCRUMB,
154
            'weight' => 10,
155
            'file' => 'field_ui.admin.inc',
156
          ) + $access_fields;
157

158
          // 'Manage display' tab.
159 160
          $items["$path/display"] = array(
            'title' => 'Manage display',
161 162
            'page callback' => 'field_ui_display_overview',
            'page arguments' => array($entity_type, $bundle_arg, 'default'),
163 164
            'type' => MENU_LOCAL_TASK,
            'weight' => 2,
165
            'file' => 'field_ui.admin.inc',
166
          ) + $access_display;
167 168 169 170 171 172 173 174 175

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

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
/**
 * Implements hook_permission().
 */
function field_ui_permission() {
  $permissions = array();

  foreach (entity_get_info() as $entity_type => $entity_info) {
    if ($entity_info['fieldable']) {
      // Create a permission for each fieldable entity to manage
      // the fields and the display.
      $permissions['administer ' . $entity_type . ' fields'] = array(
        'title' => t('%entity_label: Administer fields', array('%entity_label' => $entity_info['label'])),
        'restrict access' => TRUE,
      );
      $permissions['administer ' . $entity_type . ' display'] = array(
        'title' => t('%entity_label: Administer display', array('%entity_label' => $entity_info['label']))
      );
    }
  }

  return $permissions;
}

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

267
/**
268 269 270
 * Title callback: Returns the name of a given instance.
 *
 * @see field_ui_menu()
271
 */
272
function field_ui_instance_title($instance) {
273
  return $instance['label'];
274 275
}

276
/**
277 278 279
 * Access callback: Checks access for the 'view mode display settings' pages.
 *
 * @see field_ui_menu()
280
 */
281
function _field_ui_view_mode_menu_access($entity_type, $bundle, $view_mode, $permission) {
282 283 284 285 286
  // First, determine visibility according to the 'use custom display'
  // setting for the view mode.
  $bundle = field_extract_bundle($entity_type, $bundle);
  $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
  $visibility = ($view_mode == 'default') || !empty($view_mode_settings[$view_mode]['custom_settings']);
287

288
  // Then, determine access according to the $permission parameter.
289
  if ($visibility) {
290
    return user_access($permission);
291 292 293
  }
}

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

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

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

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

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

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

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

366 367 368 369 370
  if (!empty($bundle_name)) {
    return $inactive[$bundle_name];
  }
  return $inactive;
}
371 372

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

/**
393
 * Form submission handler for the 'Save and manage fields' button.
394 395
 *
 * @see field_ui_form_node_type_form_alter()
396 397
 */
function field_ui_form_node_type_form_submit($form, &$form_state) {
398
  if ($form_state['triggering_element']['#parents'][0] === 'save_continue') {
399
    $form_state['redirect'] = field_ui_bundle_admin_path('node', $form_state['values']['type']) .'/fields';
400 401
  }
}
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418

/**
 * Implements hook_library_info().
 */
function field_ui_library_info() {
  $libraries['drupal.field_ui'] = array(
    'title' => 'Field UI',
    'version' => VERSION,
    'js' => array(
      drupal_get_path('module', 'field_ui') . '/field_ui.js' => array(),
    ),
    'css' => array(
      drupal_get_path('module', 'field_ui') . '/field_ui.admin.css' => array(),
    ),
    'dependencies' => array(
      array('system', 'jquery'),
      array('system', 'drupal'),
419
      array('system', 'drupalSettings'),
420 421 422 423 424 425
      array('system', 'jquery.once'),
    ),
  );

  return $libraries;
}