drupal/core/modules/entity_reference/entity_reference.module

243 lines
9.2 KiB
Plaintext

<?php
/**
* @file
* Provides a field that can reference other entities.
*/
use Drupal\Component\Utility\NestedArray;
use Drupal\Core\Database\Query\AlterableInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Field\FieldDefinitionInterface;
use Drupal\Core\Render\Element;
use Drupal\field\FieldConfigInterface;
/**
* Implements hook_help().
*/
function entity_reference_help($path, $arg) {
switch ($path) {
case 'admin/help#entity_reference':
$output = '';
$output .= '<h3>' . t('About') . '</h3>';
$output .= '<p>' . t('The Entity Reference module allows you to create fields that contain links to other entities (such as content items, taxonomy terms, etc.) within the site. This allows you, for example, to include a link to a user within a content item. For more information, see <a href="!er_do">the online documentation for the Entity Reference module</a>, the <a href="!entity_help">Entity module help page</a>, and the <a href="!field_help">Field module help page</a>.', array('!field_help' => \Drupal::url('help.page', array('name' => 'field')),'!entity_help' => \Drupal::url('help.page', array('name' => 'entity')), '!er_do' => 'https://drupal.org/documentation/modules/entityreference')) . '</p>';
$output .= '<h3>' . t('Uses') . '</h3>';
$output .= '<dl>';
$output .= '<dt>' . t('Managing and displaying entity reference fields') . '</dt>';
$output .= '<dd>' . t('The <em>settings</em> and the <em>display</em> of the entity reference field can be configured separately. See the <a href="!field_ui">Field UI help</a> for more information on how to manage fields and their display.', array('!field_ui' => \Drupal::url('help.page', array('name' => 'field_ui')))) . '</dd>';
$output .= '<dt>' . t('Selecting reference type') . '</dt>';
$output .= '<dd>' . t('In the field settings you can select which entity type you want to create a reference to.') . '</dd>';
$output .= '<dt>' . t('Filtering and sorting reference fields') . '</dt>';
$output .= '<dd>' . t('Depending on the chosen entity type, additional filtering and sorting options are available for the list of entities that can be referred to, in the field settings. For example, the list of users can be filtered by role and sorted by name or ID.') . '</dd>';
$output .= '<dt>' . t('Displaying a reference') . '</dt>';
$output .= '<dd>' . t('An entity reference can be displayed as a simple label with or without a link to the entity. Alternatively, the referenced entity can be displayed as a teaser (or any other available view mode) inside the referencing entity.') . '</dd>';
$output .= '</dl>';
return $output;
}
}
/**
* Implements hook_field_info_alter().
*/
function entity_reference_field_info_alter(&$info) {
// Make the entity reference field configurable.
$info['entity_reference']['no_ui'] = FALSE;
$info['entity_reference']['class'] = '\Drupal\entity_reference\ConfigurableEntityReferenceItem';
$info['entity_reference']['list_class'] = '\Drupal\entity_reference\Plugin\Field\FieldType\ConfigurableEntityReferenceFieldItemList';
$info['entity_reference']['default_widget'] = 'entity_reference_autocomplete';
$info['entity_reference']['default_formatter'] = 'entity_reference_label';
$info['entity_reference']['provider'] = 'entity_reference';
}
/**
* Implements hook_field_widget_info_alter().
*/
function entity_reference_field_widget_info_alter(&$info) {
if (isset($info['options_select'])) {
$info['options_select']['field_types'][] = 'entity_reference';
}
if (isset($info['options_buttons'])) {
$info['options_buttons']['field_types'][] = 'entity_reference';
}
}
/**
* Implements hook_ENTITY_TYPE_update() for 'field_config'.
*
* Reset the instance handler settings, when the target type is changed.
*/
function entity_reference_field_config_update(FieldConfigInterface $field) {
if ($field->type != 'entity_reference') {
// Only act on entity reference fields.
return;
}
if ($field->isSyncing()) {
// Don't change anything during a configuration sync.
return;
}
if ($field->getSetting('target_type') == $field->original->getSetting('target_type')) {
// Target type didn't change.
return;
}
if (empty($field->bundles)) {
// Field has no instances.
return;
}
$field_name = $field->getName();
foreach ($field->bundles() as $entity_type => $bundles) {
foreach ($bundles as $bundle) {
$instance = field_info_instance($entity_type, $field_name, $bundle);
$instance->settings['handler_settings'] = array();
$instance->save();
}
}
}
/**
* Render API callback: Processes the field instance settings form and allows
* access to the form state.
*
* @see entity_reference_field_instance_settings_form()
*/
function _entity_reference_field_instance_settings_ajax_process($form, $form_state) {
_entity_reference_field_instance_settings_ajax_process_element($form, $form);
return $form;
}
/**
* Adds entity_reference specific properties to AJAX form elements from the
* field instance settings form.
*
* @see _entity_reference_field_instance_settings_ajax_process()
*/
function _entity_reference_field_instance_settings_ajax_process_element(&$element, $main_form) {
if (!empty($element['#ajax'])) {
$element['#ajax'] = array(
'callback' => 'entity_reference_settings_ajax',
'wrapper' => $main_form['#id'],
'element' => $main_form['#array_parents'],
);
}
foreach (Element::children($element) as $key) {
_entity_reference_field_instance_settings_ajax_process_element($element[$key], $main_form);
}
}
/**
* Render API callback: Moves entity_reference specific Form API elements
* (i.e. 'handler_settings') up a level for easier processing by the validation
* and submission handlers.
*
* @see _entity_reference_field_settings_process()
*/
function _entity_reference_form_process_merge_parent($element) {
$parents = $element['#parents'];
array_pop($parents);
$element['#parents'] = $parents;
return $element;
}
/**
* Form element validation handler; Filters the #value property of an element.
*/
function _entity_reference_element_validate_filter(&$element, &$form_state) {
$element['#value'] = array_filter($element['#value']);
form_set_value($element, $element['#value'], $form_state);
}
/**
* Ajax callback for the handler settings form.
*
* @see entity_reference_field_instance_settings_form()
*/
function entity_reference_settings_ajax($form, $form_state) {
$trigger = $form_state['triggering_element'];
return NestedArray::getValue($form, $trigger['#ajax']['element']);
}
/**
* Submit handler for the non-JS case.
*
* @see entity_reference_field_instance_settings_form()
*/
function entity_reference_settings_ajax_submit($form, &$form_state) {
$form_state['rebuild'] = TRUE;
}
/**
* Implements hook_query_TAG_alter().
*/
function entity_reference_query_entity_reference_alter(AlterableInterface $query) {
$handler = $query->getMetadata('entity_reference_selection_handler');
$handler->entityQueryAlter($query);
}
/**
* Creates an instance of a entity reference field on the specified bundle.
*
* @param string $entity_type
* The type of entity the field instance will be attached to.
* @param string $bundle
* The bundle name of the entity the field instance will be attached to.
* @param string $field_name
* The name of the field; if it already exists, a new instance of the existing
* field will be created.
* @param string $field_label
* The label of the field instance.
* @param string $target_entity_type
* The type of the referenced entity.
* @param string $selection_handler
* The selection handler used by this field.
* @param array $selection_handler_settings
* An array of settings supported by the selection handler specified above.
* (e.g. 'target_bundles', 'sort', 'auto_create', etc).
* @param int $cardinality
* The cardinality of the field.
*
* @see \Drupal\entity_reference\Plugin\entity_reference\selection\SelectionBase::settingsForm()
*/
function entity_reference_create_instance($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', $selection_handler_settings = array(), $cardinality = 1) {
// If a field type we know should exist isn't found, clear the field cache.
if (!\Drupal::service('plugin.manager.field.field_type')->getDefinition('entity_reference')) {
field_cache_clear();
}
// Look for or add the specified field to the requested entity bundle.
$field = field_info_field($entity_type, $field_name);
$instance = field_info_instance($entity_type, $field_name, $bundle);
if (empty($field)) {
$field = array(
'name' => $field_name,
'type' => 'entity_reference',
'entity_type' => $entity_type,
'cardinality' => $cardinality,
'settings' => array(
'target_type' => $target_entity_type,
),
);
entity_create('field_config', $field)->save();
}
if (empty($instance)) {
$instance = array(
'field_name' => $field_name,
'entity_type' => $entity_type,
'bundle' => $bundle,
'label' => $field_label,
'settings' => array(
'handler' => $selection_handler,
'handler_settings' => $selection_handler_settings,
),
);
entity_create('field_instance_config', $instance)->save();
}
}