Pathologic was missing because of a .git folder inside.
[yaffs-website] / web / modules / contrib / paragraphs / paragraphs.module
1 <?php
2
3 /**
4  * @file
5  * Contains paragraphs.module
6  */
7
8 use Drupal\Core\Url;
9 use Drupal\Core\Routing\RouteMatchInterface;
10 use Drupal\paragraphs\Entity\ParagraphsType;
11 use Drupal\Core\Render\Element;
12
13 /**
14  * Implements hook_help().
15  */
16 function paragraphs_help($route_name, RouteMatchInterface $route_match) {
17   switch ($route_name) {
18     // Main module help for the paragraphs module.
19     case 'help.page.paragraphs':
20       $output = '';
21       $output .= '<h3>' . t('About') . '</h3>';
22       $output .= '<p>' . t('The Paragraphs module provides a field type that can contain several other fields and thereby allows users to break content up on a page. Administrators can predefine <em>paragraphs types</em> (for example a simple text block, a video, or a complex and configurable slideshow). Users can then place them on a page in any order instead of using a text editor to add and configure such elements. For more information, see the <a href=":online">online documentation for the Paragraphs module</a>.', [':online' => 'https://www.drupal.org/node/2444881']) . '</p>';
23       $output .= '<h3>' . t('Uses') . '</h3>';
24       $output .= '<dt>' . t('Creating paragraphs types') . '</dt>';
25       $output .= '<dd>' . t('<em>Paragraphs types</em> can be created by clicking <em>Add paragraphs type</em> on the <a href=":paragraphs">Paragraphs types page</a>. By default a new paragraphs type does not contain any fields.', [':paragraphs' => Url::fromRoute('entity.paragraphs_type.collection')->toString()]) . '</dd>';
26       $output .= '<dt>' . t('Configuring paragraphs types') . '</dt>';
27       $output .= '<dd>' . t('Administrators can add fields to a <em>paragraphs type</em> on the <a href=":paragraphs">Paragraphs types page</a> if the <a href=":field_ui">Field UI</a> module is enabled. The form display and the display of the paragraphs type can also be managed on this page. For more information on fields and entities, see the <a href=":field">Field module help page</a>.', [':paragraphs' => Url::fromRoute('entity.paragraphs_type.collection')->toString(), ':field' => Url::fromRoute('help.page', ['name' => 'field'])->toString(), ':field_ui' => (\Drupal::moduleHandler()->moduleExists('field_ui')) ? Url::fromRoute('help.page', ['name' => 'field_ui'])->toString() : '#']) . '</dd>';
28       $output .= '<dt>' . t('Creating content with paragraphs') . '</dt>';
29       $output .= '<dd>' . t('Administrators can add a <em>paragraph</em> field to content types or other entities, and configure which <em>paragraphs types</em> to include. When users create content, they can then add one or more paragraphs by choosing the appropriate type from the dropdown list. Users can also reorder these paragraphs. This allows users to add structure to a page or other content (for example by adding an image, a user reference, or a differently formatted block of text) more easily then including it all in one text field or by using fields in a pre-defined order.') . '</dd>';
30       $output .= '<dt>' . t('Preparing to uninstalling paragraphs') . '</dt>';
31       $output .= '<dd>' . t('The Paragraphs module cannot be uninstalled when there is Paragraphs data on your website. Users with the appropriate permissions can delete all relevant data by clicking <em>Delete Paragraphs data</em> on the <a href=":uninstall">Prepare uninstall page</a>.', [':uninstall' => Url::fromRoute('paragraphs.prepare_uninstall')->toString()]) . '</dd>';
32       return $output;
33     break;
34   }
35 }
36
37 function paragraphs_type_get_types() {
38   return ParagraphsType::loadMultiple();
39 }
40
41 function paragraphs_type_get_names() {
42   return array_map(function ($bundle_info) {
43     return $bundle_info['label'];
44   }, \Drupal::service('entity_type.bundle.info')->getBundleInfo('paragraphs_type'));
45 }
46
47 function paragraphs_type_load($name) {
48   return ParagraphsType::load($name);
49 }
50
51 /**
52  * Implements hook_theme().
53  */
54 function paragraphs_theme() {
55   return array(
56     'paragraph' => array(
57       'render element' => 'elements',
58     ),
59     'paragraphs_dropbutton_wrapper' => array(
60       'variables' => array('children' => NULL),
61     ),
62   );
63 }
64
65 /**
66  * Implements hook_theme_suggestions_HOOK().
67  */
68 function paragraphs_theme_suggestions_paragraph(array $variables) {
69   $suggestions = array();
70   $paragraph = $variables['elements']['#paragraph'];
71   $sanitized_view_mode = strtr($variables['elements']['#view_mode'], '.', '_');
72
73   $suggestions[] = 'paragraph__' . $sanitized_view_mode;
74   $suggestions[] = 'paragraph__' . $paragraph->bundle();
75   $suggestions[] = 'paragraph__' . $paragraph->bundle() . '__' . $sanitized_view_mode;
76
77   return $suggestions;
78 }
79
80 /**
81  * Implements hook_form_FORM_ID_alter().
82  */
83 function paragraphs_form_entity_form_display_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
84   $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions($form['#entity_type'], $form['#bundle']);
85   // Loop over ERR field's display options with paragraph target type.
86   foreach (array_keys($field_definitions) as $field_name) {
87     if ($field_definitions[$field_name]->getType() == 'entity_reference_revisions') {
88       if ($field_definitions[$field_name]->getSettings()['target_type'] == 'paragraph') {
89         foreach (['options_buttons', 'options_select', 'entity_reference_revisions_autocomplete'] as $option) {
90           unset($form['fields'][$field_name]['plugin']['type']['#options'][$option]);
91         }
92       }
93     }
94   }
95 }
96
97 /**
98  * Implements hook_form_FORM_ID_alter().
99  */
100 function paragraphs_form_field_storage_config_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
101   if ($form_state->getFormObject()->getEntity()->getType() == 'entity_reference') {
102     // Entity Reference fields are no longer supported to reference Paragraphs.
103     unset($form['settings']['target_type']['#options'][(string) t('Content')]['paragraph']);
104   }
105 }
106
107 /**
108  * Implements hook_form_FORM_ID_alter().
109  *
110  * Indicate unsupported multilingual paragraphs field configuration.
111  */
112 function paragraphs_form_field_config_edit_form_alter(&$form,  \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
113   $field = $form_state->getFormObject()->getEntity();
114
115   if (!\Drupal::hasService('content_translation.manager')) {
116     return;
117   }
118
119   $bundle_is_translatable = \Drupal::service('content_translation.manager')
120     ->isEnabled($field->getTargetEntityTypeId(), $field->getTargetBundle());
121
122   if (!$bundle_is_translatable
123     || $field->getType() != 'entity_reference_revisions'
124     || $field->getSetting('target_type') != 'paragraph') {
125     return;
126   }
127
128   // This is a translatable ERR field pointing to a paragraph.
129   $message_display = 'warning';
130   $message_text = t('Paragraphs fields do not support translation. See the <a href=":documentation">online documentation</a>.', [
131     ':documentation' => Url::fromUri('https://www.drupal.org/node/2735121')
132       ->toString()
133   ]);
134
135   if ($form['translatable']['#default_value'] == TRUE) {
136     $message_display = 'error';
137   }
138
139   $form['paragraphs_message'] = array(
140     '#type' => 'container',
141     '#markup' => $message_text,
142     '#attributes' => array(
143       'class' => array('messages messages--' . $message_display),
144     ),
145     '#weight' => 0,
146   );
147 }
148
149 /**
150  * Implements hook_module_implements_alter().
151  *
152  * Our paragraphs_form_field_config_edit_form_alter() needs to be run after
153  * that of the content_translation module in order to see the current state
154  * of the translation field.
155  *
156  * The hook here can't be more specific, as the $hook that's passed in to this
157  * function is form_alter, and not form_FORM_ID_alter.
158  */
159 function paragraphs_module_implements_alter(&$implementations, $hook) {
160   if ($hook == 'form_alter' && isset($implementations['paragraphs'])) {
161     $group = $implementations['paragraphs'];
162     unset($implementations['paragraphs']);
163     $implementations['paragraphs'] = $group;
164   }
165 }
166
167 /**
168  * Implements hook_form_FORM_ID_alter().
169  *
170  * Indicate unsupported multilingual paragraphs field configuration.
171  *
172  * Add a warning that paragraph fields can not be translated.
173  * Switch to error if a paragraph field is marked as translatable.
174  */
175 function paragraphs_form_language_content_settings_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
176   // Without it Paragraphs message are meaningless.
177   if (!\Drupal::hasService('content_translation.manager')) {
178     return;
179   }
180
181   $content_translation_manager = \Drupal::service('content_translation.manager');
182   $message_display = 'warning';
183   $message_text = t('(* unsupported) Paragraphs fields do not support translation. See the <a href=":documentation">online documentation</a>.', [
184     ':documentation' => Url::fromUri('https://www.drupal.org/node/2735121')
185       ->toString()]);
186   $map = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('entity_reference_revisions');
187   foreach ($map as $entity_type_id => $info) {
188     if (!$content_translation_manager->isEnabled($entity_type_id)) {
189       continue;
190     }
191     $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($entity_type_id);
192     foreach ($field_storage_definitions as $name => $data) {
193       if ($data->getSetting('target_type') && $data->getSetting('target_type') == 'paragraph') {
194         foreach($data->getBundles() as $bundle) {
195           if (!$content_translation_manager->isEnabled($entity_type_id, $bundle)) {
196             continue;
197           }
198           $form['settings'][$entity_type_id][$bundle]['fields'][$name]['#label'] .= ' (* unsupported)';
199           if ($form['settings'][$entity_type_id][$bundle]['fields'][$name]['#default_value']) {
200             $message_display = 'error';
201           }
202         }
203       }
204     }
205   }
206   $form['settings']['paragraphs_message'] = array(
207     '#type' => 'container',
208     '#markup' => $message_text,
209     '#attributes' => array(
210       'class' => array('messages messages--' . $message_display),
211     ),
212     '#weight' => 0,
213   );
214   return $form;
215 }
216
217 /**
218  * Prepares variables for paragraph templates.
219  *
220  * Default template: paragraph.html.twig.
221  *
222  * Most themes use their own copy of paragraph.html.twig. The default is located
223  * inside "templates/paragraph.html.twig". Look in there for the
224  * full list of variables.
225  *
226  * @param array $variables
227  *   An associative array containing:
228  *   - elements: An array of elements to display in view mode.
229  *   - paragraph: The paragraph object.
230  *   - view_mode: View mode; e.g., 'full', 'teaser'...
231  */
232 function template_preprocess_paragraph(&$variables) {
233   $variables['view_mode'] = $variables['elements']['#view_mode'];
234   $variables['paragraph'] = $variables['elements']['#paragraph'];
235
236   // Helpful $content variable for templates.
237   $variables += array('content' => array());
238   foreach (Element::children($variables['elements']) as $key) {
239     $variables['content'][$key] = $variables['elements'][$key];
240   }
241
242   $paragraph_type = $variables['elements']['#paragraph']->getParagraphType();;
243   foreach ($paragraph_type->getEnabledBehaviorPlugins() as $plugin_id => $plugin_value) {
244     $plugin_value->preprocess($variables);
245   }
246
247 }