Upgraded imagemagick and manually altered pdf to image module to handle changes....
[yaffs-website] / web / modules / contrib / entity_reference_revisions / entity_reference_revisions.module
1 <?php
2
3 /**
4  * @file
5  * Provides a field that can reference other entities.
6  */
7
8 use Drupal\Component\Utility\NestedArray;
9 use Drupal\Core\Form\FormStateInterface;
10 use Drupal\Core\Render\Element;
11 use Drupal\Core\Routing\RouteMatchInterface;
12 use Drupal\field\Entity\FieldStorageConfig;
13 use Drupal\field\Entity\FieldConfig;
14 use Drupal\field\FieldStorageConfigInterface;
15 use Drupal\Core\Url;
16
17 /**
18  * Implements hook_help().
19  */
20 function entity_reference_revisions_help($route_name, RouteMatchInterface $route_match) {
21   switch ($route_name) {
22     case 'help.page.entity_reference_revisions':
23       $output = '';
24       $output .= '<h3>' . t('About') . '</h3>';
25       $output .= '<p>' . t('The Entity Reference Revisions 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 Revisions module</a> and the <a href=":field_help">Field module help page</a>.', [':field_help' => Url::fromRoute('help.page', ['name' => 'field'])->toString(), ':er_do' => 'https://drupal.org/documentation/modules/entity_reference_revisions']) . '</p>';
26       $output .= '<h3>' . t('Uses') . '</h3>';
27       $output .= '<dl>';
28       $output .= '<dt>' . t('Managing and displaying entity reference fields') . '</dt>';
29       $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.', [':field_ui' => Url::fromRoute('help.page', ['name' => 'field_ui'])->toString()]) . '</dd>';
30       $output .= '<dt>' . t('Selecting reference type') . '</dt>';
31       $output .= '<dd>' . t('In the field settings you can select which entity type you want to create a reference to.') . '</dd>';
32       $output .= '<dt>' . t('Filtering and sorting reference fields') . '</dt>';
33       $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>';
34       $output .= '<dt>' . t('Displaying a reference') . '</dt>';
35       $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>';
36       $output .= '</dl>';
37       return $output;
38   }
39 }
40
41 /**
42  * Implements hook_field_widget_info_alter().
43  */
44 function entity_reference_revisions_field_widget_info_alter(&$info) {
45   if (isset($info['options_select'])) {
46     $info['options_select']['field_types'][] = 'entity_reference_revisions';
47   }
48   if (isset($info['options_buttons'])) {
49     $info['options_buttons']['field_types'][] = 'entity_reference_revisions';
50   }
51 }
52
53 /**
54  * Implements hook_ENTITY_TYPE_update() for 'field_storage_config'.
55  *
56  * Reset the instance handler settings, when the target type is changed.
57  */
58 function entity_reference_revisions_field_storage_config_update(FieldStorageConfigInterface $field_storage) {
59   if ($field_storage->getType() != 'entity_reference_revisions') {
60     // Only act on entity reference fields.
61     return;
62   }
63
64   if ($field_storage->isSyncing()) {
65     // Don't change anything during a configuration sync.
66     return;
67   }
68
69   if ($field_storage->getSetting('target_type') == $field_storage->original->getSetting('target_type')) {
70     // Target type didn't change.
71     return;
72   }
73
74   if (empty($field_storage->bundles)) {
75     // Field storage has no fields.
76     return;
77   }
78
79   $field_name = $field_storage->getName();
80
81   foreach ($field_storage->bundles() as $entity_type => $bundles) {
82     foreach ($bundles as $bundle) {
83       $field = FieldConfig::loadByName($entity_type, $bundle, $field_name);
84       $field->setSetting('handler_settings', []);
85       $field->save();
86     }
87   }
88 }
89
90 /**
91  * Render API callback: Processes the field settings form and allows access to
92  * the form state.
93  *
94  * @see entity_reference_revisions_field_field_settings_form()
95  */
96 function _entity_reference_revisions_field_field_settings_ajax_process($form, FormStateInterface $form_state) {
97   _entity_reference_revisions_field_field_settings_ajax_process_element($form, $form);
98   return $form;
99 }
100
101 /**
102  * Adds entity_reference specific properties to AJAX form elements from the
103  * field settings form.
104  *
105  * @see _entity_reference_revisions_field_field_settings_ajax_process()
106  */
107 function _entity_reference_revisions_field_field_settings_ajax_process_element(&$element, $main_form) {
108   if (!empty($element['#ajax'])) {
109     $element['#ajax'] = [
110       'callback' => 'entity_reference_revisions_settings_ajax',
111       'wrapper' => $main_form['#id'],
112       'element' => $main_form['#array_parents'],
113     ];
114   }
115
116   foreach (Element::children($element) as $key) {
117     _entity_reference_revisions_field_field_settings_ajax_process_element($element[$key], $main_form);
118   }
119 }
120
121 /**
122  * Render API callback: Moves entity_reference specific Form API elements
123  * (i.e. 'handler_settings') up a level for easier processing by the validation
124  * and submission handlers.
125  *
126  * @see _entity_reference_revisions_field_settings_process()
127  */
128 function _entity_reference_revisions_form_process_merge_parent($element) {
129   $parents = $element['#parents'];
130   array_pop($parents);
131   $element['#parents'] = $parents;
132   return $element;
133 }
134
135 /**
136  * Form element validation handler; Filters the #value property of an element.
137  */
138 function _entity_reference_revisions_element_validate_filter(&$element, FormStateInterface $form_state) {
139   $element['#value'] = array_filter($element['#value']);
140   $form_state->setValueForElement($element, $element['#value']);
141 }
142
143 /**
144  * Ajax callback for the handler settings form.
145  *
146  * @see entity_reference_revisions_field_field_settings_form()
147  */
148 function entity_reference_revisions_settings_ajax($form, FormStateInterface $form_state) {
149   return NestedArray::getValue($form, $form_state->getTriggeringElement()['#ajax']['element']);
150 }
151
152 /**
153  * Submit handler for the non-JS case.
154  *
155  * @see entity_reference_revisions_field_field_settings_form()
156  */
157 function entity_reference_revisions_settings_ajax_submit($form, FormStateInterface $form_state) {
158   $form_state->setRebuild();
159 }
160
161 /**
162  * Creates a field of an entity reference revisions field storage on the specified bundle.
163  *
164  * @param string $entity_type
165  *   The type of entity the field will be attached to.
166  * @param string $bundle
167  *   The bundle name of the entity the field will be attached to.
168  * @param string $field_name
169  *   The name of the field; if it already exists, a new instance of the existing
170  *   field will be created.
171  * @param string $field_label
172  *   The label of the field.
173  * @param string $target_entity_type
174  *   The type of the referenced entity.
175  * @param string $selection_handler
176  *   The selection handler used by this field.
177  * @param array $selection_handler_settings
178  *   An array of settings supported by the selection handler specified above.
179  *   (e.g. 'target_bundles', 'sort', 'auto_create', etc).
180  * @param int $cardinality
181  *   The cardinality of the field.
182  *
183  * @see \Drupal\Core\Entity\Plugin\EntityReferenceSelection\SelectionBase::buildConfigurationForm()
184  */
185 function entity_reference_revisions_create_field($entity_type, $bundle, $field_name, $field_label, $target_entity_type, $selection_handler = 'default', $selection_handler_settings = [], $cardinality = 1) {
186   // Look for or add the specified field to the requested entity bundle.
187   if (!FieldStorageConfig::loadByName($entity_type, $field_name)) {
188     \Drupal::entityTypeManager()->getStorage('field_storage_config')->create([
189       'field_name' => $field_name,
190       'type' => 'entity_reference_revisions',
191       'entity_type' => $entity_type,
192       'cardinality' => $cardinality,
193       'settings' => [
194         'target_type' => $target_entity_type,
195       ],
196     ])->save();
197   }
198   if (!FieldConfig::loadByName($entity_type, $bundle, $field_name)) {
199     \Drupal::entityTypeManager()->getStorage('field_config')->create([
200       'field_name' => $field_name,
201       'entity_type' => $entity_type,
202       'bundle' => $bundle,
203       'label' => $field_label,
204       'settings' => [
205         'handler' => $selection_handler,
206         'handler_settings' => $selection_handler_settings,
207       ],
208     ])->save();
209   }
210 }
211
212 /**
213  * Implements hook_form_FORM_ID_alter() for 'field_ui_field_storage_add_form'.
214  */
215 function entity_reference_revisions_form_field_ui_field_storage_add_form_alter(array &$form) {
216   // Move the "Entity reference revisions" option to the end of the list and rename it to
217   // "Other".
218   unset($form['add']['new_storage_type']['#options'][(string) t('Reference revisions')]['entity_reference_revisions']);
219   $form['add']['new_storage_type']['#options'][(string) t('Reference revisions')]['entity_reference_revisions'] = t('Other…');
220 }