/** * {@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 extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state) { if ($this->isDefaultValueWidget($form_state)) { $items->filterEmptyItems(); return; } $field_name = $this->fieldDefinition->getName(); $path = array_merge($form['#parents'], array($field_name)); $submitted_values = $form_state->getValue($path); $values = []; foreach ($items as $delta => $value) { $this->setIefId(sha1($items->getName() . '-ief-single-' . $delta)); /** @var \Drupal\Core\Entity\EntityInterface $entity */ if (!($entity = $form_state->get(['inline_entity_form', $this->getIefId(), 'entity']))) { return; } $values[$submitted_values[$delta]['_weight']] = ['entity' => $entity]; } // Sort items base on weights. ksort($values); $values = array_values($values); // Let the widget massage the submitted values. $values = $this->massageFormValues($values, $form, $form_state); // Assign the values and remove the empty ones. $items->setValue($values); $items->filterEmptyItems(); // Put delta mapping in $form_state, so that flagErrors() can use it. $field_name = $this->fieldDefinition->getName(); $field_state = WidgetBase::getWidgetState($form['#parents'], $field_name, $form_state); foreach ($items as $delta => $item) { $field_state['original_deltas'][$delta] = isset($item->_original_delta) ? $item->_original_delta : $delta; unset($item->_original_delta, $item->_weight); } WidgetBase::setWidgetState($form['#parents'], $field_name, $form_state, $field_state); }
/** * {@inheritdoc} */ protected function formMultipleElements(FieldItemListInterface $items, array &$form, FormStateInterface $form_state) { // Adjust wrapper identifiers as they are shared between parents and // children in nested field collections. $form['#wrapper_id'] = Html::getUniqueID($items->getName()); $elements = parent::formMultipleElements($items, $form, $form_state); $elements['#prefix'] = '<div id="' . $form['#wrapper_id'] . '">'; $elements['#suffix'] = '</div>'; $elements['add_more']['#ajax']['wrapper'] = $form['#wrapper_id']; return $elements; }
/** * {@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; }
/** * {@inheritdoc} */ public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) { /** @var \Drupal\commerce_product\Entity\ProductInterface $product */ $product = $form_state->get('product'); $variations = $this->variationStorage->loadEnabled($product); if (count($variations) === 0) { // Nothing to purchase, tell the parent form to hide itself. $form_state->set('hide_form', TRUE); $element['variation'] = ['#type' => 'value', '#value' => 0]; return $element; } elseif (count($variations) === 1) { // Preselect the only possible variation. // @todo Limit this behavior to products with no attributes instead. $selected_variation = reset($variations); $element['variation'] = ['#type' => 'value', '#value' => $selected_variation->id()]; return $element; } // Build the full attribute form. $wrapper_id = Html::getUniqueId('commerce-product-add-to-cart-form'); $form += ['#wrapper_id' => $wrapper_id, '#prefix' => '<div id="' . $wrapper_id . '">', '#suffix' => '</div>']; $parents = array_merge($element['#field_parents'], [$items->getName(), $delta]); $user_input = (array) NestedArray::getValue($form_state->getUserInput(), $parents); $selected_variation = $this->selectVariationFromUserInput($variations, $user_input); $element['variation'] = ['#type' => 'value', '#value' => $selected_variation->id()]; $element['attributes'] = ['#type' => 'container', '#attributes' => ['class' => ['attribute-widgets']]]; foreach ($this->getAttributeInfo($selected_variation, $variations) as $field_name => $attribute) { $element['attributes'][$field_name] = ['#type' => $attribute['type'], '#title' => $attribute['title'], '#options' => $attribute['values'], '#required' => $attribute['required'], '#default_value' => $selected_variation->getAttributeId($field_name), '#ajax' => ['callback' => [get_class($this), 'ajaxRefresh'], 'wrapper' => $form['#wrapper_id']]]; // Convert the _none option into #empty_value. if (isset($element['attributes'][$field_name]['options']['_none'])) { if (!$element['attributes'][$field_name]['#required']) { $element['attributes'][$field_name]['#empty_value'] = ''; } unset($element['attributes'][$field_name]['options']['_none']); } // 1 required value -> Disable the element to skip unneeded ajax calls. if ($attribute['required'] && count($attribute['values']) === 1) { $element['attributes'][$field_name]['#disabled'] = TRUE; } } return $element; }