/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { if (!$this->canBuildForm($form_state)) { return $element; } $element['#type'] = 'fieldset'; $this->setIefId(sha1($items->getName() . '-ief-single-' . $delta)); $entity = NULL; if ($items->get($delta)->target_id) { $entity = $items->get($delta)->entity; if (!$entity) { $element['warning']['#markup'] = t('Unable to load the referenced entity.'); return $element; } } $op = isset($entity) ? 'edit' : 'add'; $language = $items->getParent()->getValue()->language()->getId(); $parents = array_merge($element['#field_parents'], [ $items->getName(), $delta, 'inline_entity_form' ]); $bundle = reset($this->getFieldSetting('handler_settings')['target_bundles']); $element['inline_entity_form'] = $this->getInlineEntityForm($op, $language, $delta, $parents, $bundle, $entity, TRUE); return $element; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { // Trick inline_entity_form_form_alter() into attaching the handlers, // WidgetSubmit will be needed once extractFormValues fills the $form_state. $parents = array_merge($element['#field_parents'], [$items->getName()]); $ief_id = sha1(implode('-', $parents)); $form_state->set(['inline_entity_form', $ief_id], []); $element['#type'] = 'fieldset'; $item = $items->get($delta); if ($item->target_id && !$item->entity) { $element['warning']['#markup'] = $this->t('Unable to load the referenced entity.'); return $element; } $entity = $item->entity; $op = $entity ? 'edit' : 'add'; $language = $items->getParent()->getValue()->language()->getId(); $parents = array_merge($element['#field_parents'], [$items->getName(), $delta, 'inline_entity_form']); $bundle = reset($this->getFieldSetting('handler_settings')['target_bundles']); $element['inline_entity_form'] = $this->getInlineEntityForm($op, $bundle, $language, $delta, $parents, $entity); if ($op == 'edit') { /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */ if (!$entity->access('update')) { // The user isn't allowed to edit the entity, but still needs to see // it, to be able to reorder values. $element['entity_label'] = ['#type' => 'markup', '#markup' => $entity->label()]; // Hide the inline form. getInlineEntityForm() still needed to be // called because otherwise the field re-ordering doesn't work. $element['inline_entity_form']['#access'] = FALSE; } } return $element; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { if (!$this->canBuildForm($form_state)) { return $element; } $this->setIefId(sha1($items->getName() . '-ief-single-' . $delta)); $entity_type = $this->getFieldSettings()['target_type']; $element['#type'] = 'fieldset'; if ($items->get($delta)->target_id) { $entity = $this->entityManager->getStorage($entity_type)->load($items->get($delta)->target_id); if ($entity) { $element['inline_entity_form'] = $this->getInlineEntityForm('edit', $entity_type, $items->getParent()->getValue()->language()->getId(), $delta, array_merge($element['#field_parents'], [$items->getName(), $delta, 'inline_entity_form']), reset($this->getFieldSettings()['handler_settings']['target_bundles']), $entity, TRUE); } else { $element['warning']['#markup'] = t('Unable to load referenced entity.'); } } else { $element['inline_entity_form'] = $this->getInlineEntityForm('add', $entity_type, $items->getParent()->getValue()->language()->getId(), $delta, array_merge($element['#field_parents'], [$items->getName(), $delta, 'inline_entity_form']), reset($this->getFieldSettings()['handler_settings']['target_bundles']), NULL, TRUE); } return $element; }
/** * {@inheritdoc} */ public function viewElements(FieldItemListInterface $items, $langcode = NULL) { $output = []; $attributes = new Attribute(); if ($this->getSetting('tag') == 'h1') { $attributes->addClass('title'); $attributes->addClass('replaced-title'); $attributes->setAttribute('id', 'page-title'); } $parent = $items->getParent()->getValue(); foreach ($items as $item) { $text = $item->getValue()['value']; if ($this->getSetting('linked')) { $text = $this->l($text, $parent->urlInfo()); } $output[] = $build['string'] = ['#type' => 'inline_template', '#template' => '<{{ tag }} {{ attributes }}>{{ text }}</{{ tag }}>', '#context' => ['text' => $text, 'tag' => $this->getSetting('tag'), 'attributes' => (string) $attributes]]; } return $output; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { if (!$this->canBuildForm($form_state)) { return $element; } $settings = $this->getSettings(); $target_type = $this->getFieldSetting('target_type'); // Get the entity type labels for the UI strings. $labels = $this->labels(); // Build a parents array for this element's values in the form. $parents = array_merge($element['#field_parents'], array($items->getName(), 'form')); // Assign a unique identifier to each IEF widget. // Since $parents can get quite long, sha1() ensures that every id has // a consistent and relatively short length while maintaining uniqueness. $this->setIefId(sha1(implode('-', $parents))); // Get the langcode of the parent entity. $parent_langcode = $items->getParent()->getValue()->language()->getId(); // Determine the wrapper ID for the entire element. $wrapper = 'inline-entity-form-' . $this->getIefId(); $element = array('#type' => 'fieldset', '#tree' => TRUE, '#description' => NULL, '#prefix' => '<div id="' . $wrapper . '">', '#suffix' => '</div>', '#ief_id' => $this->getIefId(), '#ief_root' => TRUE) + $element; $element['#attached']['library'][] = 'inline_entity_form/widget'; // Initialize the IEF array in form state. if (!$form_state->has(['inline_entity_form', $this->getIefId(), 'settings'])) { $form_state->set(['inline_entity_form', $this->getIefId(), 'settings'], $this->getFieldSettings()); } if (!$form_state->has(['inline_entity_form', $this->getIefId(), 'instance'])) { $form_state->set(['inline_entity_form', $this->getIefId(), 'instance'], $this->fieldDefinition); } if (!$form_state->has(['inline_entity_form', $this->getIefId(), 'form'])) { $form_state->set(['inline_entity_form', $this->getIefId(), 'form'], NULL); } if (!$form_state->has(['inline_entity_form', $this->getIefId(), 'array_parents'])) { $form_state->set(['inline_entity_form', $this->getIefId(), 'array_parents'], $parents); } $entities = $form_state->get(['inline_entity_form', $this->getIefId(), 'entities']); if (!isset($entities)) { // Load the entities from the $items array and store them in the form // state for further manipulation. $form_state->set(['inline_entity_form', $this->getIefId(), 'entities'], array()); if (count($items)) { foreach ($items as $delta => $item) { if ($item->entity && is_object($item->entity)) { $form_state->set(['inline_entity_form', $this->getIefId(), 'entities', $delta], array('entity' => $item->entity, '_weight' => $delta, 'form' => NULL, 'needs_save' => FALSE)); } } } $entities = $form_state->get(['inline_entity_form', $this->getIefId(), 'entities']); } // Remove any leftover data from removed entity references. foreach ($entities as $key => $value) { if (!isset($value) || !isset($value['entity'])) { unset($entities[$key]); } } // Build the "Multiple value" widget. // TODO - does this belong in #element_validate? $element['#element_validate'] = [[get_class($this), 'updateRowWeights']]; // Add the required element marker & validation. if ($element['#required']) { $element['#element_validate'][] = [get_class($this), 'requiredField']; } $element['entities'] = array('#tree' => TRUE, '#theme' => 'inline_entity_form_entity_table', '#entity_type' => $target_type); // Get the fields that should be displayed in the table. $target_bundles = $this->getTargetBundles(); $fields = $this->iefHandler->tableFields($target_bundles); $context = array('parent_entity_type' => $this->fieldDefinition->getTargetEntityTypeId(), 'parent_bundle' => $this->fieldDefinition->getTargetBundle(), 'field_name' => $this->fieldDefinition->getName(), 'entity_type' => $target_type, 'allowed_bundles' => $target_bundles); $this->moduleHandler->alter('inline_entity_form_table_fields', $fields, $context); $element['entities']['#table_fields'] = $fields; $weight_delta = max(ceil(count($entities) * 1.2), 50); foreach ($entities as $key => $value) { // Data used by theme_inline_entity_form_entity_table(). /** @var \Drupal\Core\Entity\EntityInterface $entity */ $entity = $value['entity']; $element['entities'][$key]['#entity'] = $value['entity']; $element['entities'][$key]['#needs_save'] = $value['needs_save']; // Handle row weights. $element['entities'][$key]['#weight'] = $value['_weight']; // First check to see if this entity should be displayed as a form. if (!empty($value['form'])) { $element['entities'][$key]['title'] = array(); $element['entities'][$key]['delta'] = array('#type' => 'value', '#value' => $value['_weight']); // Add the appropriate form. if ($value['form'] == 'edit') { $element['entities'][$key]['form'] = ['#type' => 'container', '#attributes' => ['class' => ['ief-form', 'ief-form-row']], 'inline_entity_form' => $this->getInlineEntityForm($value['form'], $parent_langcode, $key, array_merge($parents, ['inline_entity_form', 'entities', $key, 'form']), $entity->bundle(), $entity)]; $element['entities'][$key]['form']['inline_entity_form']['#process'] = [['\\Drupal\\inline_entity_form\\Element\\InlineEntityForm', 'processEntityForm'], [get_class($this), 'addIefSubmitCallbacks'], [get_class($this), 'buildEntityFormActions']]; } elseif ($value['form'] == 'remove') { $element['entities'][$key]['form'] = ['#type' => 'container', '#attributes' => ['class' => ['ief-form', 'ief-form-row']], '#parents' => array_merge($parents, ['entities', $key, 'form']), '#entity' => $entity, '#ief_id' => $this->getIefId(), '#ief_row_delta' => $key]; $this->buildRemoveForm($element['entities'][$key]['form']); } } else { $row =& $element['entities'][$key]; $row['title'] = array(); $row['delta'] = array('#type' => 'weight', '#delta' => $weight_delta, '#default_value' => $value['_weight'], '#attributes' => array('class' => array('ief-entity-delta'))); // Add an actions container with edit and delete buttons for the entity. $row['actions'] = array('#type' => 'container', '#attributes' => array('class' => array('ief-entity-operations'))); // Make sure entity_access is not checked for unsaved entities. $entity_id = $entity->id(); if (empty($entity_id) || $entity->access('update')) { $row['actions']['ief_entity_edit'] = array('#type' => 'submit', '#value' => $this->t('Edit'), '#name' => 'ief-' . $this->getIefId() . '-entity-edit-' . $key, '#limit_validation_errors' => array(), '#ajax' => array('callback' => 'inline_entity_form_get_element', 'wrapper' => $wrapper), '#submit' => array('inline_entity_form_open_row_form'), '#ief_row_delta' => $key, '#ief_row_form' => 'edit'); } // If 'allow_existing' is on, the default removal operation is unlink // and the access check for deleting happens inside the controller // removeForm() method. if (empty($entity_id) || $settings['allow_existing'] || $entity->access('delete')) { $row['actions']['ief_entity_remove'] = array('#type' => 'submit', '#value' => $this->t('Remove'), '#name' => 'ief-' . $this->getIefId() . '-entity-remove-' . $key, '#limit_validation_errors' => array(), '#ajax' => array('callback' => 'inline_entity_form_get_element', 'wrapper' => $wrapper), '#submit' => array('inline_entity_form_open_row_form'), '#ief_row_delta' => $key, '#ief_row_form' => 'remove'); } } } $entities_count = count($entities); $cardinality = $this->fieldDefinition->getFieldStorageDefinition()->getCardinality(); if ($cardinality > 1) { // Add a visual cue of cardinality count. $message = $this->t('You have added @entities_count out of @cardinality_count allowed @label.', array('@entities_count' => $entities_count, '@cardinality_count' => $cardinality, '@label' => $labels['plural'])); $element['cardinality_count'] = array('#markup' => '<div class="ief-cardinality-count">' . $message . '</div>'); } // Do not return the rest of the form if cardinality count has been reached. if ($cardinality > 0 && $entities_count == $cardinality) { return $element; } $target_bundles_count = count($target_bundles); $hide_cancel = FALSE; // If the field is required and empty try to open one of the forms. if (empty($entities) && $this->fieldDefinition->isRequired()) { if ($settings['allow_existing'] && !$settings['allow_new']) { $form_state->set(['inline_entity_form', $this->getIefId(), 'form'], 'ief_add_existing'); $hide_cancel = TRUE; } elseif ($target_bundles_count == 1 && $settings['allow_new'] && !$settings['allow_existing']) { $bundle = reset($target_bundles); // The parent entity type and bundle must not be the same as the inline // entity type and bundle, to prevent recursion. $parent_entity_type = $this->fieldDefinition->getTargetEntityTypeId(); $parent_bundle = $this->fieldDefinition->getTargetBundle(); if ($parent_entity_type != $target_type || $parent_bundle != $bundle) { $form_state->set(['inline_entity_form', $this->getIefId(), 'form'], 'add'); $form_state->set(['inline_entity_form', $this->getIefId(), 'form settings'], array('bundle' => $bundle)); $hide_cancel = TRUE; } } } // If no form is open, show buttons that open one. $open_form = $form_state->get(['inline_entity_form', $this->getIefId(), 'form']); if (empty($open_form)) { $element['actions'] = array('#attributes' => array('class' => array('container-inline')), '#type' => 'container', '#weight' => 100); // The user is allowed to create an entity of at least one bundle. if ($settings['allow_new'] && $target_bundles_count) { // Let the user select the bundle, if multiple are available. if ($target_bundles_count > 1) { $bundles = array(); foreach ($this->entityManager->getBundleInfo($target_type) as $bundle_name => $bundle_info) { if (in_array($bundle_name, $target_bundles)) { $bundles[$bundle_name] = $bundle_info['label']; } } $element['actions']['bundle'] = array('#type' => 'select', '#options' => $bundles); } else { $element['actions']['bundle'] = array('#type' => 'value', '#value' => reset($target_bundles)); } $element['actions']['ief_add'] = array('#type' => 'submit', '#value' => $this->t('Add new @type_singular', array('@type_singular' => $labels['singular'])), '#name' => 'ief-' . $this->getIefId() . '-add', '#limit_validation_errors' => array(array_merge($parents, array('actions'))), '#ajax' => array('callback' => 'inline_entity_form_get_element', 'wrapper' => $wrapper), '#submit' => array('inline_entity_form_open_form'), '#ief_form' => 'add'); } if ($settings['allow_existing']) { $element['actions']['ief_add_existing'] = array('#type' => 'submit', '#value' => $this->t('Add existing @type_singular', array('@type_singular' => $labels['singular'])), '#name' => 'ief-' . $this->getIefId() . '-add-existing', '#limit_validation_errors' => array(array_merge($parents, array('actions'))), '#ajax' => array('callback' => 'inline_entity_form_get_element', 'wrapper' => $wrapper), '#submit' => array('inline_entity_form_open_form'), '#ief_form' => 'ief_add_existing'); } } else { // There's a form open, show it. if ($form_state->get(['inline_entity_form', $this->getIefId(), 'form']) == 'add') { $element['form'] = ['#type' => 'fieldset', '#attributes' => ['class' => ['ief-form', 'ief-form-bottom']], 'inline_entity_form' => $this->getInlineEntityForm('add', $parent_langcode, NULL, array_merge($parents, ['inline_entity_form']), $this->determineBundle($form_state))]; $element['form']['inline_entity_form']['#process'] = [['\\Drupal\\inline_entity_form\\Element\\InlineEntityForm', 'processEntityForm'], [get_class($this), 'addIefSubmitCallbacks'], [get_class($this), 'buildEntityFormActions']]; } elseif ($form_state->get(['inline_entity_form', $this->getIefId(), 'form']) == 'ief_add_existing') { $element['form'] = array('#type' => 'fieldset', '#attributes' => array('class' => array('ief-form', 'ief-form-bottom')), '#ief_id' => $this->getIefId(), '#parents' => array_merge($parents), '#entity_type' => $target_type, '#parent_language' => $parent_langcode, '#pre_render' => [[get_class($this), 'addFieldsetMarkup']]); $element['form'] += inline_entity_form_reference_form($this->iefHandler, $element['form'], $form_state); } // Pre-opened forms can't be closed in order to force the user to // add / reference an entity. if ($hide_cancel) { if ($open_form == 'add') { $process_element =& $element['form']['inline_entity_form']; } elseif ($open_form == 'ief_add_existing') { $process_element =& $element['form']; } $process_element['#process'][] = [get_class($this), 'hideCancel']; } // No entities have been added. Remove the outer fieldset to reduce // visual noise caused by having two titles. if (empty($entities)) { $element['#type'] = 'container'; } } return $element; }