Allows administrators to attach custom fields to fieldable types.
<?php
/**
* @file
* Allows administrators to attach custom fields to fieldable types.
*/
use Drupal\entity\EntityViewModeInterface;
/**
* Implements hook_help().
*/
function field_ui_help($path, $arg) {
switch ($path) {
case 'admin/help#field_ui':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$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>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Planning fields') . '</dt>';
$output .= '<dd>' . t('There are several decisions you will need to make before defining a field for content, comments, etc.:') . '<dl>';
$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>';
$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>';
$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>';
$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>';
$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>';
$output .= '</dl>';
return $output;
case 'admin/reports/fields':
return '<p>' . t('This list shows all fields currently in use for easy reference.') . '</p>';
}
}
/**
* Implements hook_menu().
*/
function field_ui_menu() {
$items['admin/reports/fields'] = array(
'title' => 'Field list',
'description' => 'Overview of fields on all entity types.',
'page callback' => 'field_ui_fields_list',
'access arguments' => array(
'administer content types',
),
'type' => MENU_NORMAL_ITEM,
'file' => 'field_ui.admin.inc',
);
$items['admin/reports/fields/list'] = array(
'title' => 'Entities',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
// Create tabs for all possible bundles.
foreach (entity_get_info() as $entity_type => $entity_info) {
if ($entity_info['fieldable'] && isset($entity_info['route_base_path'])) {
// Extract path information from the entity type.
$path = $entity_info['route_base_path'];
$default_path = preg_replace('/{' . DRUPAL_PHP_FUNCTION_PATTERN . '}/', '%', $path);
// This is the position of the %field_ui_instance placeholder in the
// items below.
$field_position = count(explode('/', $path)) + 1;
$items["{$path}/fields"] = array(
'title' => 'Manage fields',
'type' => MENU_LOCAL_TASK,
'route_name' => "field_ui.overview.{$entity_type}",
'weight' => 1,
);
$items["{$path}/fields/%"] = array(
'title callback' => 'field_ui_instance_title',
'title arguments' => array(
$field_position,
),
'route_name' => "field_ui.instance_edit.{$entity_type}",
);
$items["{$default_path}/fields/%/edit"] = array(
'title' => 'Edit',
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$items["{$path}/fields/%/field"] = array(
'title' => 'Field settings',
'type' => MENU_LOCAL_TASK,
'route_name' => "field_ui.field_edit.{$entity_type}",
);
$items["{$path}/fields/%/widget-type"] = array(
'title' => 'Widget type',
'type' => MENU_LOCAL_TASK,
'route_name' => "field_ui.widget_type.{$entity_type}",
);
$items["{$path}/fields/%/delete"] = array(
'title' => 'Delete',
'type' => MENU_VISIBLE_IN_BREADCRUMB,
'route_name' => "field_ui.delete.{$entity_type}",
'weight' => 10,
);
// 'Manage display' tab.
$items["{$path}/display"] = array(
'title' => 'Manage display',
'type' => MENU_LOCAL_TASK,
'route_name' => "field_ui.display_overview.{$entity_type}",
'weight' => 2,
);
// 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 a route requirement to determine which ones are
// actually visible for a given bundle.
$items["{$default_path}/display/default"] = array(
'title' => t('Default'),
'type' => MENU_DEFAULT_LOCAL_TASK,
);
$weight = 0;
foreach (entity_get_view_modes($entity_type) as $view_mode => $view_mode_info) {
$items["{$path}/display/{$view_mode}"] = array(
'title' => $view_mode_info['label'],
'type' => MENU_LOCAL_TASK,
'weight' => $weight++,
'route_name' => "field_ui.display_overview.{$entity_type}.{$view_mode}",
);
}
}
}
return $items;
}
/**
* 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;
}
/**
* Menu loader callback: Loads a field instance based on field and bundle name.
*
* @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.
*
* @return
* The field instance array.
*
* @ingroup field
*/
function field_ui_instance_load($field_name, $entity_type, $bundle_name, $bundle_pos, $map) {
// 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:
// - admin/structure/types/manage/%node_type/fields/%field_ui_instance
// - admin/structure/types/manage/%comment_node_type/fields/%field_ui_instance
// 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.
if ($field = field_info_field($field_name)) {
// 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;
}
}
return FALSE;
}
/**
* Title callback: Returns the name of a given instance.
*
* @see field_ui_menu()
*/
function field_ui_instance_title($instance) {
$entity = entity_load('field_instance', $instance);
return $entity
->label();
}
/**
* Implements hook_theme().
*/
function field_ui_theme() {
return array(
'field_ui_table' => array(
'render element' => 'elements',
'file' => 'field_ui.admin.inc',
),
);
}
/**
* Implements hook_element_info().
*/
function field_ui_element_info() {
return array(
'field_ui_table' => array(
'#theme' => 'field_ui_table',
'#regions' => array(
'' => array(),
),
),
);
}
/**
* Implements hook_entity_bundle_create().
*/
function field_ui_entity_bundle_create($entity_type, $bundle) {
// When a new bundle is created, the menu needs to be rebuilt to add our
// menu item tabs.
Drupal::state()
->set('menu_rebuild_needed', TRUE);
}
/**
* Implements hook_entity_bundle_rename().
*/
function field_ui_entity_bundle_rename($entity_type, $bundle_old, $bundle_new) {
// When a bundle is renamed, the menu needs to be rebuilt to add our
// menu item tabs.
Drupal::state()
->set('menu_rebuild_needed', TRUE);
}
/**
* Implements hook_form_FORM_ID_alter().
*
* Adds a button 'Save and manage fields' to the 'Create content type' form.
*
* @see node_type_form()
* @see field_ui_form_node_type_form_submit()
*/
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)) {
$form['actions']['save_continue'] = array(
'#type' => 'submit',
'#value' => t('Save and manage fields'),
'#weight' => 45,
);
$form['#submit'][] = 'field_ui_form_node_type_form_submit';
}
}
/**
* Form submission handler for the 'Save and manage fields' button.
*
* @see field_ui_form_node_type_form_alter()
*/
function field_ui_form_node_type_form_submit($form, &$form_state) {
if ($form_state['triggering_element']['#parents'][0] === 'save_continue') {
$admin_path = Drupal::entityManager()
->getAdminPath('node', $form_state['values']['type']);
$form_state['redirect'] = "{$admin_path}/fields";
}
}
/**
* 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') . '/css/field_ui.admin.css' => array(),
),
'dependencies' => array(
array(
'system',
'jquery',
),
array(
'system',
'drupal',
),
array(
'system',
'drupalSettings',
),
array(
'system',
'jquery.once',
),
),
);
return $libraries;
}
/**
* Implements hook_view_mode_presave().
*/
function field_ui_view_mode_presave(EntityViewModeInterface $view_mode) {
Drupal::state()
->set('menu_rebuild_needed', TRUE);
}
/**
* Implements hook_view_mode_delete().
*/
function field_ui_view_mode_delete(EntityViewModeInterface $view_mode) {
Drupal::state()
->set('menu_rebuild_needed', TRUE);
}
Name | Description |
---|---|
field_ui_element_info | Implements hook_element_info(). |
field_ui_entity_bundle_create | Implements hook_entity_bundle_create(). |
field_ui_entity_bundle_rename | Implements hook_entity_bundle_rename(). |
field_ui_form_node_type_form_alter | Implements hook_form_FORM_ID_alter(). |
field_ui_form_node_type_form_submit | Form submission handler for the 'Save and manage fields' button. |
field_ui_help | Implements hook_help(). |
field_ui_instance_load | Menu loader callback: Loads a field instance based on field and bundle name. |
field_ui_instance_title | Title callback: Returns the name of a given instance. |
field_ui_library_info | Implements hook_library_info(). |
field_ui_menu | Implements hook_menu(). |
field_ui_permission | Implements hook_permission(). |
field_ui_theme | Implements hook_theme(). |
field_ui_view_mode_delete | Implements hook_view_mode_delete(). |
field_ui_view_mode_presave | Implements hook_view_mode_presave(). |