コード例 #1
0
 /**
  * {@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);
 }
コード例 #2
0
 /**
  * {@inheritdoc}
  */
 public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state)
 {
     if ($this->isDefaultValueWidget($form_state)) {
         $items->filterEmptyItems();
         return;
     }
     $trigger = $form_state->getTriggeringElement();
     if (empty($trigger['#ief_submit_trigger'])) {
         return;
     }
     $field_name = $this->fieldDefinition->getName();
     $parents = array_merge($form['#parents'], [$field_name, 'form']);
     $ief_id = sha1(implode('-', $parents));
     $this->setIefId($ief_id);
     $inline_entity_form_state = $form_state->get('inline_entity_form');
     if (isset($inline_entity_form_state[$this->getIefId()])) {
         $values = $inline_entity_form_state[$this->getIefId()];
         $key_exists = TRUE;
     } else {
         $values = [];
         $key_exists = FALSE;
     }
     if ($key_exists) {
         // If the inline entity form is still open, then its entity hasn't
         // been transfered to the IEF form state yet.
         if (empty($values['entities']) && !empty($values['form'])) {
             // @todo Do the same for reference forms.
             if ($values['form'] == 'add') {
                 $element = NestedArray::getValue($form, [$field_name, 'widget', 'form']);
                 $entity = $element['inline_entity_form']['#entity'];
                 $values['entities'][] = ['entity' => $entity];
             }
         }
         $values = $values['entities'];
         // Account for drag-and-drop reordering if needed.
         if (!$this->handlesMultipleValues()) {
             // Remove the 'value' of the 'add more' button.
             unset($values['add_more']);
             // The original delta, before drag-and-drop reordering, is needed to
             // route errors to the corect form element.
             foreach ($values as $delta => &$value) {
                 $value['_original_delta'] = $delta;
             }
             usort($values, function ($a, $b) {
                 return SortArray::sortByKeyInt($a, $b, '_weight');
             });
         }
         // 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_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);
     }
 }
コード例 #3
0
 /**
  * {@inheritdoc}
  */
 public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state)
 {
     if ($this->isDefaultValueWidget($form_state)) {
         $items->filterEmptyItems();
         return;
     }
     if (!$this->isSubmitRelevant($form, $form_state)) {
         return;
     }
     $field_name = $this->fieldDefinition->getName();
     // Extract the values from $form_state->getValues().
     $parents = array_merge($form['#parents'], [$field_name, 'form']);
     $ief_id = sha1(implode('-', $parents));
     $this->setIefId($ief_id);
     $inline_entity_form_state = $form_state->get('inline_entity_form');
     if (isset($inline_entity_form_state[$this->getIefId()])) {
         $values = $inline_entity_form_state[$this->getIefId()];
         $key_exists = TRUE;
     } else {
         $values = [];
         $key_exists = FALSE;
     }
     if ($key_exists) {
         $values = $values['entities'];
         // Account for drag-and-drop reordering if needed.
         if (!$this->handlesMultipleValues()) {
             // Remove the 'value' of the 'add more' button.
             unset($values['add_more']);
             // The original delta, before drag-and-drop reordering, is needed to
             // route errors to the corect form element.
             foreach ($values as $delta => &$value) {
                 $value['_original_delta'] = $delta;
             }
             usort($values, function ($a, $b) {
                 return SortArray::sortByKeyInt($a, $b, '_weight');
             });
         }
         foreach ($values as $delta => &$item) {
             /** @var \Drupal\Core\Entity\EntityInterface $entity */
             $entity = $item['entity'];
             if (!empty($item['needs_save'])) {
                 $entity->save();
             }
             if (!empty($item['delete'])) {
                 $entity->delete();
                 unset($items[$delta]);
             }
         }
         // 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_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);
     }
 }
コード例 #4
0
 /**
  * {@inheritdoc}
  */
 public function extractFormValues(FieldItemListInterface $items, array $form, FormStateInterface $form_state)
 {
     if ($this->isDefaultValueWidget($form_state)) {
         $items->filterEmptyItems();
         return;
     }
     $field_name = $this->fieldDefinition->getName();
     $parents = array_merge($form['#parents'], [$field_name]);
     $submitted_values = $form_state->getValue($parents);
     $values = [];
     foreach ($items as $delta => $value) {
         $element = NestedArray::getValue($form, [$field_name, 'widget', $delta]);
         /** @var \Drupal\Core\Entity\EntityInterface $entity */
         $entity = $element['inline_entity_form']['#entity'];
         $weight = isset($submitted_values[$delta]['_weight']) ? $submitted_values[$delta]['_weight'] : 0;
         $values[$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();
     // Populate the IEF form state with $items so that WidgetSubmit can
     // perform the necessary saves.
     $ief_id = sha1(implode('-', $parents));
     $widget_state = ['instance' => $this->fieldDefinition, 'delete' => [], 'entities' => []];
     foreach ($items as $delta => $value) {
         $widget_state['entities'][$delta] = ['entity' => $value->entity, 'needs_save' => TRUE];
     }
     $form_state->set(['inline_entity_form', $ief_id], $widget_state);
     // 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);
 }