/** * Initialize the form state and the entity before the first form build. */ protected function init(FormStateInterface $form_state) { // Ensure we act on the translation object corresponding to the current form // language. $langcode = $this->getFormLangcode($form_state); $this->entity = $this->entity->getTranslation($langcode); $form_display = EntityFormDisplay::collectRenderDisplay($this->entity, $this->getOperation()); $this->setFormDisplay($form_display, $form_state); parent::init($form_state); }
/** * {@inheritdoc] */ public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $entity_1 = NULL, EntityInterface $entity_2 = NULL) { // First entity. $form_state->set('entity_1', $entity_1); $form_display_1 = EntityFormDisplay::collectRenderDisplay($entity_1, 'default'); $form_state->set('form_display_1', $form_display_1); $form_display_1->buildForm($entity_1, $form, $form_state); // Second entity. $form_state->set('entity_2', $entity_2); $form_display_2 = EntityFormDisplay::collectRenderDisplay($entity_2, 'default'); $form_state->set('form_display_2', $form_display_2); $form['entity_2'] = array('#type' => 'details', '#title' => t('Second entity'), '#tree' => TRUE, '#parents' => array('entity_2'), '#weight' => 50); $form_display_2->buildForm($entity_2, $form['entity_2'], $form_state); $form['save'] = array('#type' => 'submit', '#value' => t('Save'), '#weight' => 100); return $form; }
/** * {@inheritdoc] */ public function buildForm(array $form, FormStateInterface $form_state, EntityInterface $entity_1 = NULL, EntityInterface $entity_2 = NULL) { // First entity. $form_state->set('entity_1', $entity_1); $form_display_1 = EntityFormDisplay::collectRenderDisplay($entity_1, 'default'); $form_state->set('form_display_1', $form_display_1); $form_display_1->buildForm($entity_1, $form, $form_state); // Second entity. $form_state->set('entity_2', $entity_2); $form_display_2 = EntityFormDisplay::collectRenderDisplay($entity_2, 'default'); $form_state->set('form_display_2', $form_display_2); $form['entity_2'] = ['#type' => 'details', '#title' => t('Second entity'), '#tree' => TRUE, '#parents' => ['entity_2'], '#weight' => 50, '#attributes' => ['class' => ['entity-2']]]; $form_display_2->buildForm($entity_2, $form['entity_2'], $form_state); if ($entity_2 instanceof EntityChangedInterface) { // Changed must be sent to the client, for later overwrite error checking. // @see Drupal\field\Tests\NestedFormTest::testNestedEntityFormEntityLevelValidation() $form['entity_2']['changed'] = ['#type' => 'hidden', '#default_value' => $entity_1->getChangedTime()]; } $form['save'] = array('#type' => 'submit', '#value' => t('Save'), '#weight' => 100); return $form; }
/** * Initialize the form state and the entity before the first form build. */ protected function init(FormStateInterface $form_state, EntityInterface $entity, $field_name) { // @todo Rather than special-casing $node->revision, invoke prepareEdit() // once https://www.drupal.org/node/1863258 lands. if ($entity->getEntityTypeId() == 'node') { $node_type = $this->nodeTypeStorage->load($entity->bundle()); $entity->setNewRevision($node_type->isNewRevision()); $entity->revision_log = NULL; } $form_state->set('entity', $entity); $form_state->set('field_name', $field_name); // Fetch the display used by the form. It is the display for the 'default' // form mode, with only the current field visible. $display = EntityFormDisplay::collectRenderDisplay($entity, 'default'); foreach ($display->getComponents() as $name => $options) { if ($name != $field_name) { $display->removeComponent($name); } } $form_state->set('form_display', $display); }
/** * {@inheritdoc} * * @see \Drupal\content_translation\Controller\ContentTranslationController::prepareTranslation() * Uses a similar approach to populate a new translation. */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $field_name = $this->fieldDefinition->getName(); $parents = $element['#field_parents']; $paragraphs_entity = NULL; $widget_state = static::getWidgetState($parents, $field_name, $form_state); $entity_manager = \Drupal::entityManager(); $target_type = $this->getFieldSetting('target_type'); $item_mode = isset($widget_state['paragraphs'][$delta]['mode']) ? $widget_state['paragraphs'][$delta]['mode'] : 'edit'; $default_edit_mode = $this->getSetting('edit_mode'); $show_must_be_saved_warning = FALSE; if (isset($widget_state['paragraphs'][$delta]['entity'])) { $paragraphs_entity = $widget_state['paragraphs'][$delta]['entity']; } elseif (isset($items[$delta]->entity)) { $paragraphs_entity = $items[$delta]->entity; // We don't have a widget state yet, get from selector settings. if (!isset($widget_state['paragraphs'][$delta]['mode'])) { if ($default_edit_mode == 'open') { $item_mode = 'edit'; } elseif ($default_edit_mode == 'closed') { $item_mode = 'closed'; } elseif ($default_edit_mode == 'preview') { $item_mode = 'preview'; } } } elseif (isset($widget_state['selected_bundle'])) { $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'])); $item_mode = 'edit'; } if ($item_mode == 'collapsed') { $item_mode = $default_edit_mode; $show_must_be_saved_warning = TRUE; } if ($paragraphs_entity) { // If target translation is not yet available, populate it with data from the original paragraph. $target_langcode = $this->getCurrentLangcode($form_state, $items); if ($paragraphs_entity->language()->getId() != $target_langcode && !$paragraphs_entity->hasTranslation($target_langcode)) { $paragraphs_entity->addTranslation($target_langcode, $paragraphs_entity->toArray()); } // Initiate the paragraph with the correct translation. if ($paragraphs_entity->hasTranslation($this->getCurrentLangcode($form_state, $items))) { $paragraphs_entity = $paragraphs_entity->getTranslation($this->getCurrentLangcode($form_state, $items)); } else { $paragraphs_entity = $paragraphs_entity->addTranslation($this->getCurrentLangcode($form_state, $items)); } $element_parents = $parents; $element_parents[] = $field_name; $element_parents[] = $delta; $element_parents[] = 'subform'; $id_prefix = implode('-', array_merge($parents, array($field_name, $delta))); $wrapper_id = Html::getUniqueId($id_prefix . '-item-wrapper'); $element += array('#type' => 'container', '#element_validate' => array(array($this, 'elementValidate')), 'subform' => array('#type' => 'container', '#parents' => $element_parents)); $element['#prefix'] = '<div id="' . $wrapper_id . '">'; $element['#suffix'] = '</div>'; $item_bundles = $entity_manager->getBundleInfo($target_type); if (isset($item_bundles[$paragraphs_entity->bundle()])) { $bundle_info = $item_bundles[$paragraphs_entity->bundle()]; $element['top'] = array('#type' => 'container', '#weight' => -1000, '#attributes' => array('class' => array('paragraph-type-top'))); $element['top']['paragraph_type_title'] = array('#type' => 'container', '#weight' => 0, '#attributes' => array('class' => array('paragraph-type-title'))); $element['top']['paragraph_type_title']['info'] = array('#markup' => t('@title type: %type', array('@title' => $this->getSetting('title'), '%type' => $bundle_info['label']))); $actions = array(); $links = array(); $info = array(); if ($item_mode == 'edit') { if (isset($items[$delta]->entity) && ($default_edit_mode == 'preview' || $default_edit_mode == 'closed')) { $links['collapse_button'] = array('#type' => 'submit', '#value' => t('Collapse'), '#name' => strtr($id_prefix, '-', '_') . '_collapse', '#weight' => 499, '#submit' => array(array(get_class($this), 'collapseItemSubmit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), '#delta' => $delta, '#ajax' => array('callback' => array(get_class($this), 'itemAjax'), 'wrapper' => $widget_state['ajax_wrapper_id'], 'effect' => 'fade'), '#access' => $paragraphs_entity->access('update'), '#prefix' => '<li class="collapse">', '#suffix' => '</li>'); } // Hide the button when translating. $button_access = $paragraphs_entity->access('delete') && $paragraphs_entity->language()->getId() == $paragraphs_entity->getUntranslated()->language()->getId(); $links['remove_button'] = array('#type' => 'submit', '#value' => t('Remove'), '#name' => strtr($id_prefix, '-', '_') . '_remove', '#weight' => 500, '#submit' => array(array(get_class($this), 'removeItemSubmit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), '#delta' => $delta, '#ajax' => array('callback' => array(get_class($this), 'itemAjax'), 'wrapper' => $widget_state['ajax_wrapper_id'], 'effect' => 'fade'), '#access' => $button_access, '#prefix' => '<li class="remove">', '#suffix' => '</li>'); $info['edit_button_info'] = array('#type' => 'markup', '#markup' => '<em>' . t('You are not allowed to edit this @title.', array('@title' => $this->getSetting('title'))) . '</em>', '#access' => !$paragraphs_entity->access('update') && $paragraphs_entity->access('delete')); $info['remove_button_info'] = array('#type' => 'markup', '#markup' => '<em>' . t('You are not allowed to remove this @title.', array('@title' => $this->getSetting('title'))) . '</em>', '#access' => !$paragraphs_entity->access('delete') && $paragraphs_entity->access('update')); $info['edit_remove_button_info'] = array('#type' => 'markup', '#markup' => '<em>' . t('You are not allowed to edit or remove this @title.', array('@title' => $this->getSetting('title'))) . '</em>', '#access' => !$paragraphs_entity->access('update') && !$paragraphs_entity->access('delete')); } elseif ($item_mode == 'preview' || $item_mode == 'closed') { $links['edit_button'] = array('#type' => 'submit', '#value' => t('Edit'), '#name' => strtr($id_prefix, '-', '_') . '_edit', '#weight' => 501, '#submit' => array(array(get_class($this), 'editItemSubmit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), '#delta' => $delta, '#ajax' => array('callback' => array(get_class($this), 'itemAjax'), 'wrapper' => $widget_state['ajax_wrapper_id'], 'effect' => 'fade'), '#access' => $paragraphs_entity->access('update'), '#prefix' => '<li class="edit">', '#suffix' => '</li>'); $links['remove_button'] = array('#type' => 'submit', '#value' => t('Remove'), '#name' => strtr($id_prefix, '-', '_') . '_remove', '#weight' => 502, '#submit' => array(array(get_class($this), 'removeItemSubmit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), '#delta' => $delta, '#ajax' => array('callback' => array(get_class($this), 'itemAjax'), 'wrapper' => $widget_state['ajax_wrapper_id'], 'effect' => 'fade'), '#access' => $paragraphs_entity->access('delete'), '#prefix' => '<li class="remove">', '#suffix' => '</li>'); if ($show_must_be_saved_warning) { $info['must_be_saved_info'] = array('#type' => 'markup', '#markup' => '<em>' . t('Warning: this content must be saved to reflect changes on this @title item.', array('@title' => $this->getSetting('title'))) . '</em>'); } $info['preview_info'] = array('#type' => 'markup', '#markup' => '<em>' . t('You are not allowed to view this @title.', array('@title' => $this->getSetting('title'))) . '</em>', '#access' => !$paragraphs_entity->access('view')); $info['edit_button_info'] = array('#type' => 'markup', '#markup' => '<em>' . t('You are not allowed to edit this @title.', array('@title' => $this->getSetting('title'))) . '</em>', '#access' => !$paragraphs_entity->access('update') && $paragraphs_entity->access('delete')); $info['remove_button_info'] = array('#type' => 'markup', '#markup' => '<em>' . t('You are not allowed to remove this @title.', array('@title' => $this->getSetting('title'))) . '</em>', '#access' => !$paragraphs_entity->access('delete') && $paragraphs_entity->access('update')); $info['edit_remove_button_info'] = array('#type' => 'markup', '#markup' => '<em>' . t('You are not allowed to edit or remove this @title.', array('@title' => $this->getSetting('title'))) . '</em>', '#access' => !$paragraphs_entity->access('update') && !$paragraphs_entity->access('delete')); } elseif ($item_mode == 'remove') { $element['top']['paragraph_type_title']['info'] = array('#markup' => t('Deleted @title type: %type', array('@title' => $this->getSetting('title'), '%type' => $bundle_info['label']))); $links['confirm_remove_button'] = array('#type' => 'submit', '#value' => t('Confirm removal'), '#name' => strtr($id_prefix, '-', '_') . '_confirm_remove', '#weight' => 503, '#submit' => array(array(get_class($this), 'confirmRemoveItemSubmit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), '#delta' => $delta, '#ajax' => array('callback' => array(get_class($this), 'itemAjax'), 'wrapper' => $widget_state['ajax_wrapper_id'], 'effect' => 'fade'), '#prefix' => '<li class="confirm-remove">', '#suffix' => '</li>'); $links['restore_button'] = array('#type' => 'submit', '#value' => t('Restore'), '#name' => strtr($id_prefix, '-', '_') . '_restore', '#weight' => 504, '#submit' => array(array(get_class($this), 'restoreItemSubmit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), '#delta' => $delta, '#ajax' => array('callback' => array(get_class($this), 'itemAjax'), 'wrapper' => $widget_state['ajax_wrapper_id'], 'effect' => 'fade'), '#prefix' => '<li class="restore">', '#suffix' => '</li>'); } if (count($links)) { $show_links = 0; foreach ($links as $link_item) { if (!isset($link_item['#access']) || $link_item['#access']) { $show_links++; } } if ($show_links > 0) { $element['top']['links'] = $links; if ($show_links > 1) { $element['top']['links']['#theme_wrappers'] = array('dropbutton_wrapper', 'paragraphs_dropbutton_wrapper'); $element['top']['links']['prefix'] = array('#markup' => '<ul class="dropbutton">', '#weight' => -999); $element['top']['links']['suffix'] = array('#markup' => '</li>', '#weight' => 999); } else { $element['top']['links']['#theme_wrappers'] = array('paragraphs_dropbutton_wrapper'); foreach ($links as $key => $link_item) { unset($element['top']['links'][$key]['#prefix']); unset($element['top']['links'][$key]['#suffix']); } } $element['top']['links']['#weight'] = 1; } } if (count($info)) { $show_info = FALSE; foreach ($info as $info_item) { if (!isset($info_item['#access']) || $info_item['#access']) { $show_info = TRUE; break; } } if ($show_info) { $element['info'] = $info; $element['info']['#weight'] = 998; } } if (count($actions)) { $show_actions = FALSE; foreach ($actions as $action_item) { if (!isset($action_item['#access']) || $action_item['#access']) { $show_actions = TRUE; break; } } if ($show_actions) { $element['actions'] = $actions; $element['actions']['#type'] = 'actions'; $element['actions']['#weight'] = 999; } } } $display = EntityFormDisplay::collectRenderDisplay($paragraphs_entity, $this->getSetting('form_display_mode')); if ($item_mode == 'edit') { $display->buildForm($paragraphs_entity, $element['subform'], $form_state); } elseif ($item_mode == 'preview') { $element['subform'] = array(); $element['preview'] = entity_view($paragraphs_entity, 'preview', $paragraphs_entity->language()->getId()); $element['preview']['#access'] = $paragraphs_entity->access('view'); } elseif ($item_mode == 'closed') { $element['subform'] = array(); } else { $element['subform'] = array(); } $element['subform']['#attributes']['class'][] = 'paragraphs-subform'; $element['subform']['#access'] = $paragraphs_entity->access('update'); if ($item_mode == 'removed') { $element['#access'] = FALSE; } $widget_state['paragraphs'][$delta] = array('entity' => $paragraphs_entity, 'display' => $display, 'mode' => $item_mode); static::setWidgetState($parents, $field_name, $form_state, $widget_state); } else { $element['#access'] = FALSE; } return $element; }
/** * Gets the form display for the given entity. * * @param \Drupal\Core\Entity\ContentEntityInterface $entity * The entity. * @param string $form_mode * The form mode. * * @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface * The form display. */ protected function getFormDisplay(ContentEntityInterface $entity, $form_mode) { return EntityFormDisplay::collectRenderDisplay($entity, $form_mode); }
/** * Gets the entity form display. * * @param mixed[] $element * @param \Drupal\Core\Form\FormStateInterface $form_state * * @return \Drupal\Core\Entity\Display\EntityFormDisplayInterface */ protected function getEntityFormDisplay(array $element, FormStateInterface $form_state) { $key = 'payment_reference.element.payment_reference.entity_form_display.' . $element['#name']; if (!$form_state->has($key)) { $entity_form_display = EntityFormDisplay::collectRenderDisplay($this->getPayment($element, $form_state), 'payment_reference'); $form_state->set($key, $entity_form_display); } return $form_state->get($key); }
/** * {@inheritdoc} * * @see \Drupal\content_translation\Controller\ContentTranslationController::prepareTranslation() * Uses a similar approach to populate a new translation. */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { $field_name = $this->fieldDefinition->getName(); $parents = $element['#field_parents']; $paragraphs_entity = NULL; $host = $items->getEntity(); $widget_state = static::getWidgetState($parents, $field_name, $form_state); $entity_manager = \Drupal::entityTypeManager(); $target_type = $this->getFieldSetting('target_type'); $item_mode = isset($widget_state['paragraphs'][$delta]['mode']) ? $widget_state['paragraphs'][$delta]['mode'] : 'edit'; $default_edit_mode = $this->getSetting('edit_mode'); $show_must_be_saved_warning = FALSE; if (isset($widget_state['paragraphs'][$delta]['entity'])) { $paragraphs_entity = $widget_state['paragraphs'][$delta]['entity']; } elseif (isset($items[$delta]->entity)) { $paragraphs_entity = $items[$delta]->entity; // We don't have a widget state yet, get from selector settings. if (!isset($widget_state['paragraphs'][$delta]['mode'])) { if ($default_edit_mode == 'open') { $item_mode = 'edit'; } elseif ($default_edit_mode == 'closed') { $item_mode = 'closed'; } elseif ($default_edit_mode == 'preview') { $item_mode = 'preview'; } } } elseif (isset($widget_state['selected_bundle'])) { $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'])); $item_mode = 'edit'; } if ($item_mode == 'collapsed') { $item_mode = $default_edit_mode; $show_must_be_saved_warning = TRUE; } 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. if ($paragraphs_entity->get('langcode') != $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', $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); } } $element_parents = $parents; $element_parents[] = $field_name; $element_parents[] = $delta; $element_parents[] = 'subform'; $id_prefix = implode('-', array_merge($parents, array($field_name, $delta))); $wrapper_id = Html::getUniqueId($id_prefix . '-item-wrapper'); $element += array('#type' => 'container', '#element_validate' => array(array($this, 'elementValidate')), 'subform' => array('#type' => 'container', '#parents' => $element_parents)); $element['#prefix'] = '<div id="' . $wrapper_id . '">'; $element['#suffix'] = '</div>'; $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()]; $element['top'] = array('#type' => 'container', '#weight' => -1000, '#attributes' => array('class' => array('paragraph-type-top'))); $element['top']['paragraph_type_title'] = array('#type' => 'container', '#weight' => 0, '#attributes' => array('class' => array('paragraph-type-title'))); $element['top']['paragraph_type_title']['info'] = array('#markup' => '<strong>Type: ' . $bundle_info['label'] . '</strong>'); $actions = array(); $links = array(); $info = array(); if ($item_mode == 'edit') { if (isset($items[$delta]->entity) && ($default_edit_mode == 'preview' || $default_edit_mode == 'closed')) { $links['collapse_button'] = array('#type' => 'submit', '#value' => $this->t('Collapse'), '#name' => strtr($id_prefix, '-', '_') . '_collapse', '#weight' => 499, '#submit' => array(array(get_class($this), 'paragraphsItemSubmit')), '#delta' => $delta, '#ajax' => array('callback' => array(get_class($this), 'itemAjax'), 'wrapper' => $widget_state['ajax_wrapper_id'], 'effect' => 'fade'), '#access' => $paragraphs_entity->access('update'), '#prefix' => '<li class="collapse">', '#suffix' => '</li>', '#paragraphs_mode' => 'collapsed'); } // Hide the button when translating. $button_access = $paragraphs_entity->access('delete') && !$this->isTranslating; $links['remove_button'] = array('#type' => 'submit', '#value' => $this->t('Remove'), '#name' => strtr($id_prefix, '-', '_') . '_remove', '#weight' => 500, '#submit' => array(array(get_class($this), 'paragraphsItemSubmit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), '#delta' => $delta, '#ajax' => array('callback' => array(get_class($this), 'itemAjax'), 'wrapper' => $widget_state['ajax_wrapper_id'], 'effect' => 'fade'), '#access' => $button_access, '#prefix' => '<li class="remove">', '#suffix' => '</li>', '#paragraphs_mode' => 'remove'); $info['edit_button_info'] = array('#type' => 'markup', '#markup' => '<em>' . $this->t('You are not allowed to edit this @title.', array('@title' => $this->getSetting('title'))) . '</em>', '#access' => !$paragraphs_entity->access('update') && $paragraphs_entity->access('delete')); $info['remove_button_info'] = array('#type' => 'markup', '#markup' => '<em>' . $this->t('You are not allowed to remove this @title.', array('@title' => $this->getSetting('title'))) . '</em>', '#access' => !$paragraphs_entity->access('delete') && $paragraphs_entity->access('update')); $info['edit_remove_button_info'] = array('#type' => 'markup', '#markup' => '<em>' . $this->t('You are not allowed to edit or remove this @title.', array('@title' => $this->getSetting('title'))) . '</em>', '#access' => !$paragraphs_entity->access('update') && !$paragraphs_entity->access('delete')); } elseif ($item_mode == 'preview' || $item_mode == 'closed') { $links['edit_button'] = array('#type' => 'submit', '#value' => $this->t('Edit'), '#name' => strtr($id_prefix, '-', '_') . '_edit', '#weight' => 501, '#submit' => array(array(get_class($this), 'paragraphsItemSubmit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), '#delta' => $delta, '#ajax' => array('callback' => array(get_class($this), 'itemAjax'), 'wrapper' => $widget_state['ajax_wrapper_id'], 'effect' => 'fade'), '#access' => $paragraphs_entity->access('update'), '#prefix' => '<li class="edit">', '#suffix' => '</li>', '#paragraphs_mode' => 'edit'); $links['remove_button'] = array('#type' => 'submit', '#value' => $this->t('Remove'), '#name' => strtr($id_prefix, '-', '_') . '_remove', '#weight' => 502, '#submit' => array(array(get_class($this), 'paragraphsItemSubmit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), '#delta' => $delta, '#ajax' => array('callback' => array(get_class($this), 'itemAjax'), 'wrapper' => $widget_state['ajax_wrapper_id'], 'effect' => 'fade'), '#access' => $paragraphs_entity->access('delete'), '#prefix' => '<li class="remove">', '#suffix' => '</li>', '#paragraphs_mode' => 'remove'); if ($show_must_be_saved_warning) { $info['must_be_saved_info'] = array('#type' => 'markup', '#markup' => '<em>' . $this->t('Warning: this content must be saved to reflect changes on this @title item.', array('@title' => $this->getSetting('title'))) . '</em>'); } $info['preview_info'] = array('#type' => 'markup', '#markup' => '<em>' . $this->t('You are not allowed to view this @title.', array('@title' => $this->getSetting('title'))) . '</em>', '#access' => !$paragraphs_entity->access('view')); $info['edit_button_info'] = array('#type' => 'markup', '#markup' => '<em>' . $this->t('You are not allowed to edit this @title.', array('@title' => $this->getSetting('title'))) . '</em>', '#access' => !$paragraphs_entity->access('update') && $paragraphs_entity->access('delete')); $info['remove_button_info'] = array('#type' => 'markup', '#markup' => '<em>' . $this->t('You are not allowed to remove this @title.', array('@title' => $this->getSetting('title'))) . '</em>', '#access' => !$paragraphs_entity->access('delete') && $paragraphs_entity->access('update')); $info['edit_remove_button_info'] = array('#type' => 'markup', '#markup' => '<em>' . $this->t('You are not allowed to edit or remove this @title.', array('@title' => $this->getSetting('title'))) . '</em>', '#access' => !$paragraphs_entity->access('update') && !$paragraphs_entity->access('delete')); } elseif ($item_mode == 'remove') { $element['top']['paragraph_type_title']['info'] = array('#markup' => $this->t('Deleted @title: %type', ['@title' => $this->getSetting('title'), '%type' => $bundle_info['label']])); $links['confirm_remove_button'] = array('#type' => 'submit', '#value' => $this->t('Confirm removal'), '#name' => strtr($id_prefix, '-', '_') . '_confirm_remove', '#weight' => 503, '#submit' => array(array(get_class($this), 'paragraphsItemSubmit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), '#delta' => $delta, '#ajax' => array('callback' => array(get_class($this), 'itemAjax'), 'wrapper' => $widget_state['ajax_wrapper_id'], 'effect' => 'fade'), '#prefix' => '<li class="confirm-remove">', '#suffix' => '</li>', '#paragraphs_mode' => 'removed'); $links['restore_button'] = array('#type' => 'submit', '#value' => $this->t('Restore'), '#name' => strtr($id_prefix, '-', '_') . '_restore', '#weight' => 504, '#submit' => array(array(get_class($this), 'paragraphsItemSubmit')), '#limit_validation_errors' => array(array_merge($parents, array($field_name, 'add_more'))), '#delta' => $delta, '#ajax' => array('callback' => array(get_class($this), 'itemAjax'), 'wrapper' => $widget_state['ajax_wrapper_id'], 'effect' => 'fade'), '#prefix' => '<li class="restore">', '#suffix' => '</li>', '#paragraphs_mode' => 'edit'); } if (count($links)) { $show_links = 0; foreach ($links as $link_item) { if (!isset($link_item['#access']) || $link_item['#access']) { $show_links++; } } if ($show_links > 0) { $element['top']['links'] = $links; if ($show_links > 1) { $element['top']['links']['#theme_wrappers'] = array('dropbutton_wrapper', 'paragraphs_dropbutton_wrapper'); $element['top']['links']['prefix'] = array('#markup' => '<ul class="dropbutton">', '#weight' => -999); $element['top']['links']['suffix'] = array('#markup' => '</li>', '#weight' => 999); } else { $element['top']['links']['#theme_wrappers'] = array('paragraphs_dropbutton_wrapper'); foreach ($links as $key => $link_item) { unset($element['top']['links'][$key]['#prefix']); unset($element['top']['links'][$key]['#suffix']); } } $element['top']['links']['#weight'] = 1; } } if (count($info)) { $show_info = FALSE; foreach ($info as $info_item) { if (!isset($info_item['#access']) || $info_item['#access']) { $show_info = TRUE; break; } } if ($show_info) { $element['info'] = $info; $element['info']['#weight'] = 998; } } if (count($actions)) { $show_actions = FALSE; foreach ($actions as $action_item) { if (!isset($action_item['#access']) || $action_item['#access']) { $show_actions = TRUE; break; } } if ($show_actions) { $element['actions'] = $actions; $element['actions']['#type'] = 'actions'; $element['actions']['#weight'] = 999; } } } $display = EntityFormDisplay::collectRenderDisplay($paragraphs_entity, $this->getSetting('form_display_mode')); if ($item_mode == 'edit') { $display->buildForm($paragraphs_entity, $element['subform'], $form_state); foreach (Element::children($element['subform']) as $field) { if ($paragraphs_entity->hasField($field)) { $translatable = $paragraphs_entity->{$field}->getFieldDefinition()->isTranslatable(); if ($translatable) { $element['subform'][$field]['widget']['#after_build'][] = [static::class, 'removeTranslatabilityClue']; } } } } elseif ($item_mode == 'preview') { $element['subform'] = array(); $element['preview'] = entity_view($paragraphs_entity, 'preview', $paragraphs_entity->language()->getId()); $element['preview']['#access'] = $paragraphs_entity->access('view'); } elseif ($item_mode == 'closed') { $element['subform'] = array(); } else { $element['subform'] = array(); } $element['subform']['#attributes']['class'][] = 'paragraphs-subform'; $element['subform']['#access'] = $paragraphs_entity->access('update'); if ($item_mode == 'removed') { $element['#access'] = FALSE; } $widget_state['paragraphs'][$delta] = array('entity' => $paragraphs_entity, 'display' => $display, 'mode' => $item_mode); static::setWidgetState($parents, $field_name, $form_state, $widget_state); } else { $element['#access'] = FALSE; } return $element; }