Пример #1
0
 /**
  * {@inheritdoc}
  */
 public function buildConfigurationForm(array $form, FormStateInterface $form_state)
 {
     $default_entity_type = $form_state->getValue('entity_type', $this->configuration['entity_type']);
     $default_display = $form_state->getValue('display', $this->configuration['display']);
     $default_display_settings = $form_state->getValue('display_settings', $this->configuration['display_settings']);
     $default_display_settings += ['entity_type' => $default_entity_type];
     $form['#prefix'] = '<div id="multi-step-form-wrapper">';
     $form['#suffix'] = '</div>';
     $entity_types = [];
     foreach ($this->entityManager->getDefinitions() as $entity_type_id => $entity_type) {
         /** @var \Drupal\Core\Entity\EntityTypeInterface $entity_type */
         $entity_types[$entity_type_id] = $entity_type->getLabel();
     }
     $form['entity_type'] = ['#type' => 'select', '#title' => $this->t('Entity type'), '#description' => $this->t("Entity browser itself does not need information about entity type being selected. It can actually select entities of different type. However, some of the display plugins need to know which entity type they are operating with. Display plugins that do not need this info will ignore this configuration value."), '#default_value' => $default_entity_type, '#options' => $entity_types, '#ajax' => ['callback' => [$this, 'updateSettingsAjax'], 'wrapper' => 'multi-step-form-wrapper']];
     $displays = [];
     foreach ($this->fieldDisplayManager->getDefinitions() as $display_plugin_id => $definition) {
         $entity_type = $this->entityManager->getDefinition($default_entity_type);
         if ($this->fieldDisplayManager->createInstance($display_plugin_id)->isApplicable($entity_type)) {
             $displays[$display_plugin_id] = $definition['label'];
         }
     }
     $form['display'] = ['#title' => t('Entity display plugin'), '#type' => 'select', '#default_value' => $default_display, '#options' => $displays, '#ajax' => ['callback' => [$this, 'updateSettingsAjax'], 'wrapper' => 'multi-step-form-wrapper']];
     $form['display_settings'] = ['#type' => 'container', '#title' => t('Entity display plugin configuration'), '#tree' => TRUE];
     if ($default_display_settings) {
         $display_plugin = $this->fieldDisplayManager->createInstance($default_display, $default_display_settings);
         $form['display_settings'] += $display_plugin->settingsForm($form, $form_state);
     }
     return $form;
 }
Пример #2
0
 /**
  * Builds the render array for displaying the current results.
  *
  * @param string $details_id
  *   The ID for the details element.
  * @param string[] $field_parents
  *   Field parents.
  * @param \Drupal\Core\Entity\ContentEntityInterface[] $entities
  *
  * @return array
  *   The render array for the current selection.
  */
 protected function displayCurrentSelection($details_id, $field_parents, $entities)
 {
     $field_widget_display = $this->fieldDisplayManager->createInstance($this->getSetting('field_widget_display'), $this->getSetting('field_widget_display_settings') + ['entity_type' => $this->fieldDefinition->getFieldStorageDefinition()->getSetting('target_type')]);
     return ['#theme_wrappers' => ['container'], '#attributes' => ['class' => ['entities-list']], 'items' => array_map(function (ContentEntityInterface $entity) use($field_widget_display, $details_id, $field_parents) {
         $display = $field_widget_display->view($entity);
         if (is_string($display)) {
             $display = ['#markup' => $display];
         }
         return ['#theme_wrappers' => ['container'], '#attributes' => ['class' => ['item-container', Html::getClass($field_widget_display->getPluginId())], 'data-entity-id' => $entity->id()], 'display' => $display, 'remove_button' => ['#type' => 'submit', '#value' => $this->t('Remove'), '#ajax' => ['callback' => [get_class($this), 'updateWidgetCallback'], 'wrapper' => $details_id], '#submit' => [[get_class($this), 'removeItemSubmit']], '#name' => $this->fieldDefinition->getName() . '_remove_' . $entity->id(), '#limit_validation_errors' => [array_merge($field_parents, [$this->fieldDefinition->getName()])], '#attributes' => ['data-entity-id' => $entity->id()], '#access' => (bool) $this->getSetting('field_widget_remove')], 'edit_button' => ['#type' => 'submit', '#value' => $this->t('Edit'), '#ajax' => ['url' => Url::fromRoute('entity_browser.edit_form', ['entity_type' => $entity->getEntityTypeId(), 'entity' => $entity->id()])], '#access' => (bool) $this->getSetting('field_widget_edit')]];
     }, $entities)];
 }
Пример #3
0
 /**
  * {@inheritdoc}
  */
 function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state)
 {
     $entity_type = $this->fieldDefinition->getFieldStorageDefinition()->getSetting('target_type');
     $entity_storage = $this->entityManager->getStorage($entity_type);
     $field_widget_display = $this->fieldDisplayManager->createInstance($this->getSetting('field_widget_display'), $this->getSetting('field_widget_display_settings') + ['entity_type' => $this->fieldDefinition->getFieldStorageDefinition()->getSetting('target_type')]);
     $ids = [];
     if (($trigger = $form_state->getTriggeringElement()) && in_array($this->fieldDefinition->getName(), $trigger['#parents'])) {
         // Submit was triggered by hidden "target_id" element when entities were
         // added via entity browser.
         if (!empty($trigger['#ajax']['event']) && $trigger['#ajax']['event'] == 'entity_browser_value_updated') {
             $parents = $trigger['#parents'];
         } elseif ($trigger['#type'] == 'submit' && strpos($trigger['#name'], $this->fieldDefinition->getName() . '_remove_') === 0) {
             $parents = array_merge(array_slice($trigger['#parents'], 0, -4), ['target_id']);
         }
         if (isset($parents) && ($value = $form_state->getValue($parents))) {
             $ids = explode(' ', $value);
             $entities = $entity_storage->loadMultiple($ids);
         }
     } else {
         foreach ($items as $item) {
             $entity = $entity_storage->load($item->target_id);
             if (!empty($entity)) {
                 $entities[$item->target_id] = $entity;
             }
         }
         $ids = array_keys($entities);
     }
     $ids = array_filter($ids);
     $hidden_id = Html::getUniqueId('edit-' . $this->fieldDefinition->getName() . '-target-id');
     $details_id = Html::getUniqueId('edit-' . $this->fieldDefinition->getName());
     /** @var \Drupal\entity_browser\EntityBrowserInterface $entity_browser */
     $entity_browser = $this->entityManager->getStorage('entity_browser')->load($this->getSetting('entity_browser'));
     $element += ['#id' => $details_id, '#type' => 'details', '#open' => !empty($ids), 'target_id' => ['#type' => 'hidden', '#id' => $hidden_id, '#attributes' => ['id' => $hidden_id], '#default_value' => $ids, '#ajax' => ['callback' => [get_class($this), 'updateWidgetCallback'], 'wrapper' => $details_id, 'event' => 'entity_browser_value_updated']]];
     $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality();
     if ($cardinality == FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED || count($ids) < $cardinality) {
         $element['entity_browser'] = $entity_browser->getDisplay()->displayEntityBrowser();
         $element['#attached']['library'][] = 'entity_browser/entity_reference';
         $element['#attached']['drupalSettings']['entity_browser'] = [$entity_browser->getDisplay()->getUuid() => ['cardinality' => $this->fieldDefinition->getFieldStorageDefinition()->getCardinality(), 'selector' => '#' . $element['target_id']['#attributes']['id']]];
     }
     $field_parents = $element['#field_parents'];
     $element['current'] = ['#theme_wrappers' => ['container'], '#attributes' => ['class' => ['entities-list']], 'items' => array_map(function ($id) use($entity_storage, $field_widget_display, $details_id, $field_parents, $entities) {
         $entity = $entities[$id];
         $display = $field_widget_display->view($entity);
         if (is_string($display)) {
             $display = ['#markup' => $display];
         }
         return ['#theme_wrappers' => ['container'], '#attributes' => ['class' => ['item-container'], 'data-entity-id' => $entity->id()], 'display' => $display, 'remove_button' => ['#type' => 'submit', '#value' => $this->t('Remove'), '#ajax' => ['callback' => [get_class($this), 'updateWidgetCallback'], 'wrapper' => $details_id], '#submit' => [[get_class($this), 'removeItemSubmit']], '#name' => $this->fieldDefinition->getName() . '_remove_' . $id, '#limit_validation_errors' => [array_merge($field_parents, [$this->fieldDefinition->getName()])], '#attributes' => ['data-entity-id' => $id]]];
     }, $ids)];
     return $element;
 }