diff --git a/paragraphs.module b/paragraphs.module index 7d73bfd..020f2ff 100644 --- a/paragraphs.module +++ b/paragraphs.module @@ -95,58 +95,6 @@ function paragraphs_form_entity_form_display_edit_form_alter(&$form, \Drupal\Cor } /** - * Implements hook_form_FORM_ID_alter(). - */ -function paragraphs_form_field_storage_config_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { - if ($form_state->getFormObject()->getEntity()->getType() == 'entity_reference') { - // Entity Reference fields are no longer supported to reference Paragraphs. - unset($form['settings']['target_type']['#options'][(string) t('Content')]['paragraph']); - } -} - -/** - * Implements hook_form_FORM_ID_alter(). - * - * Indicate unsupported multilingual paragraphs field configuration. - */ -function paragraphs_form_field_config_edit_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { - $field = $form_state->getFormObject()->getEntity(); - - if (!\Drupal::hasService('content_translation.manager')) { - return; - } - - $bundle_is_translatable = \Drupal::service('content_translation.manager') - ->isEnabled($field->getTargetEntityTypeId(), $field->getTargetBundle()); - - if (!$bundle_is_translatable - || $field->getType() != 'entity_reference_revisions' - || $field->getSetting('target_type') != 'paragraph') { - return; - } - - // This is a translatable ERR field pointing to a paragraph. - $message_display = 'warning'; - $message_text = t('Paragraphs fields do not support translation. See the online documentation.', [ - ':documentation' => Url::fromUri('https://www.drupal.org/node/2735121') - ->toString() - ]); - - if ($form['translatable']['#default_value'] == TRUE) { - $message_display = 'error'; - } - - $form['paragraphs_message'] = array( - '#type' => 'container', - '#markup' => $message_text, - '#attributes' => array( - 'class' => array('messages messages--' . $message_display), - ), - '#weight' => 0, - ); -} - -/** * Implements hook_module_implements_alter(). * * Our paragraphs_form_field_config_edit_form_alter() needs to be run after @@ -165,56 +113,6 @@ function paragraphs_module_implements_alter(&$implementations, $hook) { } /** - * Implements hook_form_FORM_ID_alter(). - * - * Indicate unsupported multilingual paragraphs field configuration. - * - * Add a warning that paragraph fields can not be translated. - * Switch to error if a paragraph field is marked as translatable. - */ -function paragraphs_form_language_content_settings_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) { - // Without it Paragraphs message are meaningless. - if (!\Drupal::hasService('content_translation.manager')) { - return; - } - - $content_translation_manager = \Drupal::service('content_translation.manager'); - $message_display = 'warning'; - $message_text = t('(* unsupported) Paragraphs fields do not support translation. See the online documentation.', [ - ':documentation' => Url::fromUri('https://www.drupal.org/node/2735121') - ->toString()]); - $map = \Drupal::service('entity_field.manager')->getFieldMapByFieldType('entity_reference_revisions'); - foreach ($map as $entity_type_id => $info) { - if (!$content_translation_manager->isEnabled($entity_type_id)) { - 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) { - 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']) { - $message_display = 'error'; - } - } - } - } - } - $form['settings']['paragraphs_message'] = array( - '#type' => 'container', - '#markup' => $message_text, - '#attributes' => array( - 'class' => array('messages messages--' . $message_display), - ), - '#weight' => 0, - ); - return $form; -} - -/** * Prepares variables for paragraph templates. * * Default template: paragraph.html.twig. diff --git a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php index 35aac3f..e9c202d 100644 --- a/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php +++ b/src/Plugin/Field/FieldWidget/InlineParagraphsWidget.php @@ -4,6 +4,7 @@ namespace Drupal\paragraphs\Plugin\Field\FieldWidget; use Drupal\Component\Utility\NestedArray; use Drupal\Component\Utility\Html; +use Drupal\Core\Entity\ContentEntityInterface; use Drupal\Core\Entity\Entity\EntityFormDisplay; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\RevisionableInterface; @@ -13,7 +14,9 @@ use Drupal\Core\Field\WidgetBase; use Drupal\Core\Form\FormStateInterface; use Drupal\Core\Field\FieldItemListInterface; use Drupal\Core\Render\Element; +use Drupal\field\Entity\FieldConfig; use Drupal\paragraphs; +use Drupal\paragraphs\Entity\Paragraph; use Symfony\Component\Validator\ConstraintViolationInterface; @@ -140,14 +143,15 @@ class InlineParagraphsWidget extends WidgetBase { $elements['form_display_mode'] = array( '#type' => 'select', - '#options' => \Drupal::service('entity_display.repository')->getFormModeOptions($this->getFieldSetting('target_type')), + '#options' => \Drupal::service('entity_display.repository') + ->getFormModeOptions($this->getFieldSetting('target_type')), '#description' => $this->t('The form display mode to use when rendering the paragraph form.'), '#title' => $this->t('Form display mode'), '#default_value' => $this->getSetting('form_display_mode'), '#required' => TRUE, ); - $options = []; + $options = []; foreach ($this->getAllowedTypes() as $key => $bundle) { $options[$key] = $bundle['label']; } @@ -174,7 +178,7 @@ class InlineParagraphsWidget extends WidgetBase { '@title_plural' => $this->getSetting('title_plural') ]); - switch($this->getSetting('edit_mode')) { + switch ($this->getSetting('edit_mode')) { case 'open': default: $edit_mode = $this->t('Open'); @@ -187,7 +191,7 @@ class InlineParagraphsWidget extends WidgetBase { break; } - switch($this->getSetting('add_mode')) { + switch ($this->getSetting('add_mode')) { case 'select': default: $add_mode = $this->t('Select list'); @@ -226,7 +230,6 @@ class InlineParagraphsWidget extends WidgetBase { $info = []; $paragraphs_entity = NULL; - $host = $items->getEntity(); $widget_state = static::getWidgetState($parents, $field_name, $form_state); $entity_manager = \Drupal::entityTypeManager(); @@ -262,9 +265,10 @@ class InlineParagraphsWidget extends WidgetBase { $entity_type = $entity_manager->getDefinition($target_type); $bundle_key = $entity_type->getKey('bundle'); - $paragraphs_entity = $entity_manager->getStorage($target_type)->create(array( - $bundle_key => $widget_state['selected_bundle'], - )); + $paragraphs_entity = $entity_manager->getStorage($target_type) + ->create(array( + $bundle_key => $widget_state['selected_bundle'], + )); $item_mode = 'edit'; } @@ -293,55 +297,17 @@ class InlineParagraphsWidget extends WidgetBase { } if ($paragraphs_entity) { - // Detect if we are translating. - $this->initIsTranslating($form_state, $host); - $langcode = $form_state->get('langcode'); - - if (!$this->isTranslating) { - // Set the langcode if we are not translating. - $langcode_key = $paragraphs_entity->getEntityType()->getKey('langcode'); - if ($paragraphs_entity->get($langcode_key)->value != $langcode) { - // If a translation in the given language already exists, switch to - // that. If there is none yet, update the language. - if ($paragraphs_entity->hasTranslation($langcode)) { - $paragraphs_entity = $paragraphs_entity->getTranslation($langcode); - } - else { - $paragraphs_entity->set($langcode_key, $langcode); - } - } - } - else { - // Add translation if missing for the target language. - if (!$paragraphs_entity->hasTranslation($langcode)) { - // Get the selected translation of the paragraph entity. - $entity_langcode = $paragraphs_entity->language()->getId(); - $source = $form_state->get(['content_translation', 'source']); - $source_langcode = $source ? $source->getId() : $entity_langcode; - $paragraphs_entity = $paragraphs_entity->getTranslation($source_langcode); - // The paragraphs entity has no content translation source field if - // no paragraph entity field is translatable, even if the host is. - if ($paragraphs_entity->hasField('content_translation_source')) { - // Initialise the translation with source language values. - $paragraphs_entity->addTranslation($langcode, $paragraphs_entity->toArray()); - $translation = $paragraphs_entity->getTranslation($langcode); - $manager = \Drupal::service('content_translation.manager'); - $manager->getTranslationMetadata($translation)->setSource($paragraphs_entity->language()->getId()); - } - } - // If any paragraphs type is translatable do not switch. - if ($paragraphs_entity->hasField('content_translation_source')) { - // Switch the paragraph to the translation. - $paragraphs_entity = $paragraphs_entity->getTranslation($langcode); - } - } + $paragraphs_entity = $this->prepareEntity($paragraphs_entity, $items, $form_state); $element_parents = $parents; $element_parents[] = $field_name; $element_parents[] = $delta; $element_parents[] = 'subform'; - $id_prefix = implode('-', array_merge($parents, array($field_name, $delta))); + $id_prefix = implode('-', array_merge($parents, array( + $field_name, + $delta + ))); $wrapper_id = Html::getUniqueId($id_prefix . '-item-wrapper'); $element += array( @@ -356,7 +322,8 @@ class InlineParagraphsWidget extends WidgetBase { $element['#prefix'] = '
'; $element['#suffix'] = '
'; - $item_bundles = \Drupal::service('entity_type.bundle.info')->getBundleInfo($target_type); + $item_bundles = \Drupal::service('entity_type.bundle.info') + ->getBundleInfo($target_type); if (isset($item_bundles[$paragraphs_entity->bundle()])) { $bundle_info = $item_bundles[$paragraphs_entity->bundle()]; @@ -388,7 +355,8 @@ class InlineParagraphsWidget extends WidgetBase { $links = array(); // Hide the button when translating. - $button_access = $paragraphs_entity->access('delete') && !$this->isTranslating; + $button_access = $paragraphs_entity->access('delete') && (!$this->isTranslating || $items->getFieldDefinition() + ->isTranslatable()); if ($item_mode != 'remove') { $links['remove_button'] = [ '#type' => 'submit', @@ -419,7 +387,12 @@ class InlineParagraphsWidget extends WidgetBase { '#value' => $this->t('Collapse'), '#name' => strtr($id_prefix, '-', '_') . '_collapse', '#weight' => 499, - '#submit' => array(array(get_class($this), 'paragraphsItemSubmit')), + '#submit' => array( + array( + get_class($this), + 'paragraphsItemSubmit' + ) + ), '#delta' => $delta, '#ajax' => array( 'callback' => array(get_class($this), 'itemAjax'), @@ -465,7 +438,12 @@ class InlineParagraphsWidget extends WidgetBase { '#name' => strtr($id_prefix, '-', '_') . '_edit', '#weight' => 500, '#submit' => array(array(get_class($this), 'paragraphsItemSubmit')), - '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), + '#limit_validation_errors' => array( + array_merge($parents, array( + $field_name, + 'add_more' + )) + ), '#delta' => $delta, '#ajax' => array( 'callback' => array(get_class($this), 'itemAjax'), @@ -559,7 +537,7 @@ class InlineParagraphsWidget extends WidgetBase { if (count($links)) { $show_links = 0; - foreach($links as $link_item) { + foreach ($links as $link_item) { if (!isset($link_item['#access']) || $link_item['#access']) { $show_links++; } @@ -569,7 +547,10 @@ class InlineParagraphsWidget extends WidgetBase { $element['top']['links'] = $links; if ($show_links > 1) { - $element['top']['links']['#theme_wrappers'] = array('dropbutton_wrapper', 'paragraphs_dropbutton_wrapper'); + $element['top']['links']['#theme_wrappers'] = array( + 'dropbutton_wrapper', + 'paragraphs_dropbutton_wrapper' + ); $element['top']['links']['prefix'] = array( '#markup' => '