Updated to Drupal 8.5. Core Media not yet in use.
[yaffs-website] / web / modules / contrib / paragraphs / paragraphs.module
index 7d73bfdcca092e09faa2a92c251b5a6a64dde7f1..1cd3570d0a9799d2977aab633b9b21a62eee5a5a 100644 (file)
@@ -5,8 +5,10 @@
  * Contains paragraphs.module
  */
 
+use Drupal\Core\Field\FieldConfigInterface;
 use Drupal\Core\Url;
 use Drupal\Core\Routing\RouteMatchInterface;
+use Drupal\field\FieldStorageConfigInterface;
 use Drupal\paragraphs\Entity\ParagraphsType;
 use Drupal\Core\Render\Element;
 
@@ -19,16 +21,14 @@ function paragraphs_help($route_name, RouteMatchInterface $route_match) {
     case 'help.page.paragraphs':
       $output = '';
       $output .= '<h3>' . t('About') . '</h3>';
-      $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>';
+      $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>';
       $output .= '<h3>' . t('Uses') . '</h3>';
-      $output .= '<dt>' . t('Creating paragraphs types') . '</dt>';
-      $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>';
-      $output .= '<dt>' . t('Configuring paragraphs types') . '</dt>';
-      $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>';
-      $output .= '<dt>' . t('Creating content with paragraphs') . '</dt>';
-      $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>';
-      $output .= '<dt>' . t('Preparing to uninstalling paragraphs') . '</dt>';
-      $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>';
+      $output .= '<dt>' . t('Creating Paragraphs types') . '</dt>';
+      $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>';
+      $output .= '<dt>' . t('Configuring Paragraphs types') . '</dt>';
+      $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>';
+      $output .= '<dt>' . t('Creating content with Paragraphs') . '</dt>';
+      $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 dragdrop 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>';
       return $output;
     break;
   }
@@ -59,6 +59,20 @@ function paragraphs_theme() {
     'paragraphs_dropbutton_wrapper' => array(
       'variables' => array('children' => NULL),
     ),
+    'paragraphs_info_icon' => [
+      'variables' => [
+        'message' => NULL,
+        'icon' => NULL,
+      ],
+    ],
+    'paragraphs_add_dialog' => [
+      'render element' => 'element',
+      'template' => 'paragraphs-add-dialog',
+    ],
+    'paragraphs_actions' => [
+      'render element' => 'element',
+      'template' => 'paragraphs-actions',
+    ],
   );
 }
 
@@ -189,14 +203,32 @@ function paragraphs_form_language_content_settings_form_alter(&$form, \Drupal\Co
       continue;
     }
     $field_storage_definitions = \Drupal::service('entity_field.manager')->getFieldStorageDefinitions($entity_type_id);
-    foreach ($field_storage_definitions as $name => $data) {
-      if ($data->getSetting('target_type') && $data->getSetting('target_type') == 'paragraph') {
-        foreach($data->getBundles() as $bundle) {
+
+    /** @var \Drupal\Core\Field\FieldStorageDefinitionInterface  $storage_definition */
+    foreach ($field_storage_definitions as $name => $storage_definition) {
+      if ($storage_definition->getSetting('target_type') && $storage_definition->getSetting('target_type') == 'paragraph') {
+
+        // For configurable fields, check all bundles on which the field exists,
+        // for base fields that are translable, check all bundles,
+        // untranslatable base fields do not show up at all.
+        $bundles = [];
+        if ($storage_definition instanceof FieldStorageConfigInterface) {
+          $bundles = $storage_definition->getBundles();
+        }
+        elseif ($storage_definition->isTranslatable()) {
+          $bundles = Element::children($form['settings'][$entity_type_id]);
+        }
+        foreach($bundles as $bundle) {
           if (!$content_translation_manager->isEnabled($entity_type_id, $bundle)) {
             continue;
           }
-          $form['settings'][$entity_type_id][$bundle]['fields'][$name]['#label'] .= ' (* unsupported)';
-          if ($form['settings'][$entity_type_id][$bundle]['fields'][$name]['#default_value']) {
+
+          // Update the label and if the paragraph field is translatable,
+          // display an error message instead of just a warning.
+          if (isset($form['settings'][$entity_type_id][$bundle]['fields'][$name]['#label'])) {
+            $form['settings'][$entity_type_id][$bundle]['fields'][$name]['#label'] = t('@field_label (* unsupported)', ['@field_label' => $form['settings'][$entity_type_id][$bundle]['fields'][$name]['#label']]);
+          }
+          if (!empty($form['settings'][$entity_type_id][$bundle]['fields'][$name]['#default_value'])) {
             $message_display = 'error';
           }
         }
@@ -211,7 +243,6 @@ function paragraphs_form_language_content_settings_form_alter(&$form, \Drupal\Co
     ),
     '#weight' => 0,
   );
-  return $form;
 }
 
 /**
@@ -239,9 +270,155 @@ function template_preprocess_paragraph(&$variables) {
     $variables['content'][$key] = $variables['elements'][$key];
   }
 
-  $paragraph_type = $variables['elements']['#paragraph']->getParagraphType();;
+  $paragraph_type = $variables['elements']['#paragraph']->getParagraphType();
   foreach ($paragraph_type->getEnabledBehaviorPlugins() as $plugin_id => $plugin_value) {
     $plugin_value->preprocess($variables);
   }
 
 }
+
+/**
+ * Prepares variables for modal form add widget template.
+ *
+ * Default template: paragraphs-add-dialog.html.twig
+ *
+ * @param array $variables
+ *   An associative array containing:
+ *   - buttons: An array of buttons to display in the modal form.
+ */
+function template_preprocess_paragraphs_add_dialog(&$variables) {
+  // Define variables for the template.
+  $variables += ['buttons' => []];
+  foreach (Element::children($variables['element']) as $key) {
+    if ($key == 'add_modal_form_area') {
+      // $add variable for the add button.
+      $variables['add'] = $variables['element'][$key];
+    }
+    else {
+      // Buttons for the paragraph types in the modal form.
+      $variables['buttons'][$key] = $variables['element'][$key];
+    }
+  }
+}
+
+/**
+ * Prepares variables for paragraphs_actions component.
+ *
+ * Default template: paragraphs-actions.html.twig.
+ *
+ * @param array $variables
+ *   An associative array containing:
+ *   - actions: An array of default action buttons.
+ *   - dropdown_actions: An array of buttons for dropdown.
+ */
+function template_preprocess_paragraphs_actions(&$variables) {
+  // Define variables for the template.
+  $variables += ['actions' => [], 'dropdown_actions' => []];
+
+  $element = $variables['element'];
+
+  if (!empty($element['actions'])) {
+    $variables['actions'] = $element['actions'];
+  }
+
+  if (!empty($element['dropdown_actions'])) {
+    $variables['dropdown_actions'] = $element['dropdown_actions'];
+  }
+}
+
+/**
+ * Implements hook_preprocess_HOOK() for field_multiple_value_form().
+ */
+function paragraphs_preprocess_field_multiple_value_form(&$variables) {
+  if (count($variables['element']['#field_parents']) === 0 && isset($variables['table']['#rows'])) {
+    // Find paragraph_actions and move to header.
+    // @see template_preprocess_field_multiple_value_form()
+    if (!empty($variables['table']['#rows'][0]['data'][1]['data']['#paragraphs_header'])) {
+      $variables['table']['#header'][0]['data'] = [
+        'title' => $variables['table']['#header'][0]['data'],
+        'button' => $variables['table']['#rows'][0]['data'][1]['data'],
+      ];
+      unset($variables['table']['#rows'][0]);
+    }
+  }
+}
+
+/**
+ * Implements hook_libraries_info().
+ */
+function paragraphs_libraries_info() {
+  $libraries = [
+    'Sortable' => [
+      'name' => 'Sortable',
+      'vendor url' => 'https://github.com/RubaXa/Sortable',
+      'download url' => 'https://github.com/RubaXa/Sortable/releases',
+      'files' => [
+        'js' => [
+          'Sortable.min.js' => [],
+        ],
+      ],
+      'version arguments' => [
+        // The version is at the end of the file, which is currently about 15k
+        // characters long.
+        'file' => 'Sortable.min.js',
+        'pattern' => '/\.version="(.*?)"/',
+        'lines' => 2,
+        'cols' => 20000
+      ]
+    ],
+  ];
+  return $libraries;
+}
+
+/**
+ * Implements hook_library_info_alter().
+ */
+function paragraphs_library_info_alter(&$libraries, $extension) {
+  if ($extension != 'paragraphs') {
+    return;
+  }
+
+  if (\Drupal::moduleHandler()->moduleExists('libraries')) {
+    $info = libraries_detect('Sortable');
+  }
+  else {
+    // If the library module is not installed, hardcode the path and fetch
+    // the required information ourself.
+    $library_path = 'libraries/Sortable';
+    $file = 'Sortable.min.js';
+    $path = DRUPAL_ROOT . '/' . $library_path . '/' . $file;
+    if (file_exists($path)) {
+      if (preg_match('/\.version="(.*?)"/', file_get_contents($path), $version)) {
+        $info = [
+          'installed' => TRUE,
+          'version' => $version[1],
+          'library path' => $library_path,
+          'files' => [
+            'js' => [
+              $file => [],
+            ],
+          ],
+        ];
+      }
+    }
+  }
+
+  if (!empty($info['installed'])) {
+    $libraries['sortable'] += [
+      'version' => $info['version'],
+    ];
+    // Self hosted player, use files from library definition.
+    if (!empty($info['files']['js'])) {
+      foreach ($info['files']['js'] as $filename => $options) {
+        $libraries['sortable']['js']["/{$info['library path']}/{$filename}"] = $options;
+      }
+    }
+  }
+  else {
+    // Unset the libraries if we failed to detect them.
+    unset($libraries['sortable']);
+    unset($libraries['paragraphs-dragdrop']);
+  }
+
+  return $libraries;
+}