Drupal PHP Cross Reference Content Management Systems

Source: /modules/field_ui/field_ui.module - 387 lines - 20352 bytes - Summary - Text - Print

   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 site configuration'),
 110            );
 111  
 112            $items["$path/fields"] = array(
 113              'title' => 'Manage fields',
 114              'page callback' => 'drupal_get_form',
 115              'page arguments' => array('field_ui_field_overview_form', $entity_type, $bundle_arg),
 116              'type' => MENU_LOCAL_TASK,
 117              'weight' => 1,
 118              'file' => 'field_ui.admin.inc',
 119            ) + $access;
 120            $items["$path/fields/%field_ui_menu"] = array(
 121              'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
 122              'title callback' => 'field_ui_menu_title',
 123              'title arguments' => array($field_position),
 124              'page callback' => 'drupal_get_form',
 125              'page arguments' => array('field_ui_field_edit_form', $field_position),
 126              'file' => 'field_ui.admin.inc',
 127            ) + $access;
 128            $items["$path/fields/%field_ui_menu/edit"] = array(
 129              'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
 130              'title' => 'Edit',
 131              'page callback' => 'drupal_get_form',
 132              'page arguments' => array('field_ui_field_edit_form', $field_position),
 133              'type' => MENU_DEFAULT_LOCAL_TASK,
 134              'file' => 'field_ui.admin.inc',
 135            ) + $access;
 136            $items["$path/fields/%field_ui_menu/field-settings"] = array(
 137              'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
 138              'title' => 'Field settings',
 139              'page callback' => 'drupal_get_form',
 140              'page arguments' => array('field_ui_field_settings_form', $field_position),
 141              'type' => MENU_LOCAL_TASK,
 142              'file' => 'field_ui.admin.inc',
 143            ) + $access;
 144            $items["$path/fields/%field_ui_menu/widget-type"] = array(
 145              'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
 146              'title' => 'Widget type',
 147              'page callback' => 'drupal_get_form',
 148              'page arguments' => array('field_ui_widget_type_form', $field_position),
 149              'type' => MENU_LOCAL_TASK,
 150              'file' => 'field_ui.admin.inc',
 151            ) + $access;
 152            $items["$path/fields/%field_ui_menu/delete"] = array(
 153              'load arguments' => array($entity_type, $bundle_arg, $bundle_pos, '%map'),
 154              'title' => 'Delete',
 155              'page callback' => 'drupal_get_form',
 156              'page arguments' => array('field_ui_field_delete_form', $field_position),
 157              'type' => MENU_LOCAL_TASK,
 158              'weight' => 10,
 159              'file' => 'field_ui.admin.inc',
 160            ) + $access;
 161  
 162            // 'Manage display' tab.
 163            $items["$path/display"] = array(
 164              'title' => 'Manage display',
 165              'page callback' => 'drupal_get_form',
 166              'page arguments' => array('field_ui_display_overview_form', $entity_type, $bundle_arg, 'default'),
 167              'type' => MENU_LOCAL_TASK,
 168              'weight' => 2,
 169              'file' => 'field_ui.admin.inc',
 170            ) + $access;
 171  
 172            // View modes secondary tabs.
 173            // The same base $path for the menu item (with a placeholder) can be
 174            // used for all bundles of a given entity type; but depending on
 175            // administrator settings, each bundle has a different set of view
 176            // modes available for customisation. So we define menu items for all
 177            // view modes, and use an access callback to determine which ones are
 178            // actually visible for a given bundle.
 179            $weight = 0;
 180            $view_modes = array('default' => array('label' => t('Default'))) + $entity_info['view modes'];
 181            foreach ($view_modes as $view_mode => $view_mode_info) {
 182              $items["$path/display/$view_mode"] = array(
 183                'title' => $view_mode_info['label'],
 184                'page arguments' => array('field_ui_display_overview_form', $entity_type, $bundle_arg, $view_mode),
 185                // The access callback needs to check both the current 'custom
 186                // display' setting for the view mode, and the overall access
 187                // rules for the bundle admin pages.
 188                'access callback' => '_field_ui_view_mode_menu_access',
 189                'access arguments' => array_merge(array($entity_type, $bundle_arg, $view_mode, $access['access callback']), $access['access arguments']),
 190                'type' => ($view_mode == 'default' ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK),
 191                'weight' => ($view_mode == 'default' ? -10 : $weight++),
 192                'file' => 'field_ui.admin.inc',
 193              );
 194            }
 195          }
 196        }
 197      }
 198    }
 199    return $items;
 200  }
 201  
 202  /**
 203   * Menu loader; Load a field instance based on field and bundle name.
 204   *
 205   * @param $field_name
 206   *   The name of the field, as contained in the path.
 207   * @param $entity_type
 208   *   The name of the entity.
 209   * @param $bundle_name
 210   *   The name of the bundle, as contained in the path.
 211   * @param $bundle_pos
 212   *   The position of $bundle_name in $map.
 213   * @param $map
 214   *   The translated menu router path argument map.
 215   *
 216   * @return
 217   *   The field instance array.
 218   *
 219   * @ingroup field
 220   */
 221  function field_ui_menu_load($field_name, $entity_type, $bundle_name, $bundle_pos, $map) {
 222    // Extract the actual bundle name from the translated argument map.
 223    // The menu router path to manage fields of an entity can be shared among
 224    // multiple bundles. For example:
 225    // - admin/structure/types/manage/%node_type/fields/%field_ui_menu
 226    // - admin/structure/types/manage/%comment_node_type/fields/%field_ui_menu
 227    // The menu system will automatically load the correct bundle depending on the
 228    // actual path arguments, but this menu loader function only receives the node
 229    // type string as $bundle_name, which is not the bundle name for comments.
 230    // We therefore leverage the dynamically translated $map provided by the menu
 231    // system to retrieve the actual bundle and bundle name for the current path.
 232    if ($bundle_pos > 0) {
 233      $bundle = $map[$bundle_pos];
 234      $bundle_name = field_extract_bundle($entity_type, $bundle);
 235    }
 236    // Check whether the field exists at all.
 237    if ($field = field_info_field($field_name)) {
 238      // Only return the field if a field instance exists for the given entity
 239      // type and bundle.
 240      if ($instance = field_info_instance($entity_type, $field_name, $bundle_name)) {
 241        return $instance;
 242      }
 243    }
 244    return FALSE;
 245  }
 246  
 247  /**
 248   * Menu title callback.
 249   */
 250  function field_ui_menu_title($instance) {
 251    return $instance['label'];
 252  }
 253  
 254  /**
 255   * Menu access callback for the 'view mode display settings' pages.
 256   */
 257  function _field_ui_view_mode_menu_access($entity_type, $bundle, $view_mode, $access_callback) {
 258    // First, determine visibility according to the 'use custom display'
 259    // setting for the view mode.
 260    $bundle = field_extract_bundle($entity_type, $bundle);
 261    $view_mode_settings = field_view_mode_settings($entity_type, $bundle);
 262    $visibility = ($view_mode == 'default') || !empty($view_mode_settings[$view_mode]['custom_settings']);
 263  
 264    // Then, determine access according to the $access parameter. This duplicates
 265    // part of _menu_check_access().
 266    if ($visibility) {
 267      // Grab the variable 'access arguments' part.
 268      $all_args = func_get_args();
 269      $args = array_slice($all_args, 4);
 270      $callback = empty($access_callback) ? 0 : trim($access_callback);
 271      if (is_numeric($callback)) {
 272        return (bool) $callback;
 273      }
 274      else {
 275        // As call_user_func_array() is quite slow and user_access is a very
 276        // common callback, it is worth making a special case for it.
 277        if ($access_callback == 'user_access') {
 278          return (count($args) == 1) ? user_access($args[0]) : user_access($args[0], $args[1]);
 279        }
 280        elseif (function_exists($access_callback)) {
 281          return call_user_func_array($access_callback, $args);
 282        }
 283      }
 284    }
 285  }
 286  
 287  /**
 288   * Implements hook_theme().
 289   */
 290  function field_ui_theme() {
 291    return array(
 292      'field_ui_table' => array(
 293        'render element' => 'elements',
 294      ),
 295    );
 296  }
 297  
 298  /**
 299   * Implements hook_element_info().
 300   */
 301  function field_ui_element_info() {
 302    return array(
 303      'field_ui_table' => array(
 304        '#theme' => 'field_ui_table',
 305        '#pre_render' => array('field_ui_table_pre_render'),
 306        '#regions' => array('' => array()),
 307      ),
 308    );
 309  }
 310  
 311  /**
 312   * Implements hook_field_attach_create_bundle().
 313   */
 314  function field_ui_field_attach_create_bundle($entity_type, $bundle) {
 315    // When a new bundle is created, the menu needs to be rebuilt to add our
 316    // menu item tabs.
 317    variable_set('menu_rebuild_needed', TRUE);
 318  }
 319  
 320  /**
 321   * Determines the adminstration path for a bundle.
 322   */
 323  function _field_ui_bundle_admin_path($entity_type, $bundle_name) {
 324    $bundles = field_info_bundles($entity_type);
 325    $bundle_info = $bundles[$bundle_name];
 326    if (isset($bundle_info['admin'])) {
 327      return isset($bundle_info['admin']['real path']) ? $bundle_info['admin']['real path'] : $bundle_info['admin']['path'];
 328    }
 329  }
 330  
 331  /**
 332   * Identifies inactive fields within a bundle.
 333   */
 334  function field_ui_inactive_instances($entity_type, $bundle_name = NULL) {
 335    if (!empty($bundle_name)) {
 336      $inactive = array($bundle_name => array());
 337      $params = array('bundle' => $bundle_name);
 338    }
 339    else {
 340      $inactive = array();
 341      $params = array();
 342    }
 343    $params['entity_type'] = $entity_type;
 344  
 345    $active_instances = field_info_instances($entity_type);
 346    $all_instances = field_read_instances($params, array('include_inactive' => TRUE));
 347    foreach ($all_instances as $instance) {
 348      if (!isset($active_instances[$instance['bundle']][$instance['field_name']])) {
 349        $inactive[$instance['bundle']][$instance['field_name']] = $instance;
 350      }
 351    }
 352    if (!empty($bundle_name)) {
 353      return $inactive[$bundle_name];
 354    }
 355    return $inactive;
 356  }
 357  
 358  /**
 359   * Implements hook_form_FORM_ID_alter().
 360   *
 361   * Adds a button 'Save and add fields' to the 'Create content type' form.
 362   *
 363   * @see node_type_form()
 364   * @see field_ui_form_node_type_form_submit()
 365   */
 366  function field_ui_form_node_type_form_alter(&$form, $form_state) {
 367    // We want to display the button only on add page.
 368    if (empty($form['#node_type']->type)) {
 369      $form['actions']['save_continue'] = array(
 370        '#type' => 'submit',
 371        '#value' => t('Save and add fields'),
 372        '#weight' => 45,
 373      );
 374      $form['#submit'][] = 'field_ui_form_node_type_form_submit';
 375    }
 376  }
 377  
 378  /**
 379   * Form submission handler for the 'Save and add fields' button.
 380   *
 381   * @see field_ui_form_node_type_form_alter()
 382   */
 383  function field_ui_form_node_type_form_submit($form, &$form_state) {
 384    if ($form_state['triggering_element']['#parents'][0] === 'save_continue') {
 385      $form_state['redirect'] = _field_ui_bundle_admin_path('node', $form_state['values']['type']) .'/fields';
 386    }
 387  }

title

Description

title

Description

title

Description

title

title

Body