Commit b30e657b authored by catch's avatar catch

Issue #1846648 by yched, swentel: Rename field_attach_submit(), because it is...

Issue #1846648 by yched, swentel: Rename field_attach_submit(), because it is also called from within validation.
parent a17c5355
......@@ -505,10 +505,10 @@ function entity_get_form(EntityInterface $entity, $operation = 'default', $langc
* for copying the form values of added form elements to entity properties.
* Many of the main entity builder functions can call this helper function to
* re-use its logic of copying $form_state['values'][PROPERTY] values to
* $entity->PROPERTY for all entries in $form_state['values'] that are not field
* data, and calling field_attach_submit() to copy field data. Apart from that
* this helper invokes any additional builder functions that have been specified
* in $form['#entity_builders'].
* $entity->PROPERTY for all entries in $form_state['values'] that are not
* field data, and calling field_attach_extract_form_values() to copy field
* data. Apart from that this helper invokes any additional builder functions
* that have been specified in $form['#entity_builders'].
*
* For some entity forms (e.g., forms with complex non-field data and forms that
* simultaneously edit multiple entities), this behavior may be inappropriate,
......@@ -520,7 +520,8 @@ function entity_form_submit_build_entity($entity_type, $entity, $form, &$form_st
// Copy top-level form values that are not for fields to entity properties,
// without changing existing entity properties that are not being edited by
// this form. Copying field values must be done using field_attach_submit().
// this form. Copying field values must be done using
// field_attach_extract_form_values().
$values_excluding_fields = $info['fieldable'] ? array_diff_key($form_state['values'], field_info_instances($entity_type, $entity->bundle())) : $form_state['values'];
foreach ($values_excluding_fields as $key => $value) {
$entity->set($key, $value);
......@@ -535,7 +536,7 @@ function entity_form_submit_build_entity($entity_type, $entity, $form, &$form_st
// Copy field values to the entity.
if ($info['fieldable']) {
field_attach_submit($entity, $form, $form_state);
field_attach_extract_form_values($entity, $form, $form_state);
}
}
......
......@@ -73,7 +73,7 @@ public function buildEntity(array $form, array &$form_state) {
// Copy top-level form values that are entity fields but not handled by
// field API without changing existing entity fields that are not being
// edited by this form. Values of fields handled by field API are copied
// by field_attach_submit() below.
// by field_attach_extract_form_values() below.
$values_excluding_fields = $info['fieldable'] ? array_diff_key($form_state['values'], field_info_instances($entity_type, $entity->bundle())) : $form_state['values'];
$translation = $entity->getTranslation($this->getFormLangcode($form_state), FALSE);
$definitions = $translation->getPropertyDefinitions();
......@@ -92,7 +92,7 @@ public function buildEntity(array $form, array &$form_state) {
// Invoke field API for copying field values.
if ($info['fieldable']) {
field_attach_submit($entity->getBCEntity(), $form, $form_state);
field_attach_extract_form_values($entity->getBCEntity(), $form, $form_state);
}
return $entity;
}
......
......@@ -83,10 +83,7 @@ public function submit(array $form, array &$form_state) {
protected function buildEntity(array $form, array &$form_state) {
$entity = clone $form_state['entity'];
// @todo field_attach_submit() only "submits" to the in-memory $entity
// object, not to anywhere persistent. Consider renaming it to minimize
// confusion: http://drupal.org/node/1846648.
field_attach_submit($entity, $form, $form_state, array('field_name' => $form_state['field_name']));
field_attach_extract_form_values($entity, $form, $form_state, array('field_name' => $form_state['field_name']));
// @todo Refine automated log messages and abstract them to all entity
// types: http://drupal.org/node/1678002.
......
......@@ -886,7 +886,8 @@ function hook_field_formatter_info_alter(array &$info) {
*/
function hook_field_attach_form(\Drupal\Core\Entity\EntityInterface $entity, &$form, &$form_state, $langcode) {
// Add a checkbox allowing a given field to be emptied.
// See hook_field_attach_submit() for the corresponding processing code.
// See hook_field_attach_extract_form_values() for the corresponding
// processing code.
$form['empty_field_foo'] = array(
'#type' => 'checkbox',
'#title' => t("Empty the 'field_foo' field"),
......@@ -932,7 +933,7 @@ function hook_field_attach_validate(\Drupal\Core\Entity\EntityInterface $entity,
}
/**
* Act on field_attach_submit().
* Act on field_attach_extract_form_values().
*
* This hook is invoked after the field module has performed the operation.
*
......@@ -947,7 +948,7 @@ function hook_field_attach_validate(\Drupal\Core\Entity\EntityInterface $entity,
* @param $form_state
* An associative array containing the current state of the form.
*/
function hook_field_attach_submit(\Drupal\Core\Entity\EntityInterface $entity, $form, &$form_state) {
function hook_field_attach_extract_form_values(\Drupal\Core\Entity\EntityInterface $entity, $form, &$form_state) {
// Sample case of an 'Empty the field' checkbox added on the form, allowing
// a given field to be emptied.
$values = NestedArray::getValue($form_state['values'], $form['#parents']);
......
......@@ -727,8 +727,8 @@ function _field_invoke_widget_target() {
* appear within the same $form element, or within the same '#parents' space.
*
* For each call to field_attach_form(), field values are processed by calling
* field_attach_form_validate() and field_attach_submit() on the same $form
* element.
* field_attach_form_validate() and field_attach_extract_form_values() on the
* same $form element.
*
* Sample resulting structure in $form:
* @code
......@@ -1096,7 +1096,7 @@ function field_attach_form_validate(EntityInterface $entity, $form, &$form_state
}
/**
* Performs necessary operations on field data submitted by a form.
* Populates an entity object with values from a form submission.
*
* Currently, this accounts for drag-and-drop reordering of field values, and
* filtering of empty values.
......@@ -1113,14 +1113,14 @@ function field_attach_form_validate(EntityInterface $entity, $form, &$form_state
* An associative array of additional options. See field_invoke_method() for
* details.
*/
function field_attach_submit(EntityInterface $entity, $form, &$form_state, array $options = array()) {
function field_attach_extract_form_values(EntityInterface $entity, $form, &$form_state, array $options = array()) {
// Extract field values from submitted values.
field_invoke_method('submit', _field_invoke_widget_target(), $entity, $form, $form_state, $options);
field_invoke_method('extractFormValues', _field_invoke_widget_target(), $entity, $form, $form_state, $options);
// Let other modules act on submitting the entity.
// Avoid module_invoke_all() to let $form_state be taken by reference.
foreach (module_implements('field_attach_submit') as $module) {
$function = $module . '_field_attach_submit';
foreach (module_implements('field_attach_extract_form_values') as $module) {
$function = $module . 'field_attach_extract_form_values';
$function($entity, $form, $form_state);
}
}
......
......@@ -306,9 +306,9 @@ protected function formSingleElement(EntityInterface $entity, array $items, $del
}
/**
* Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::submit().
* Implements Drupal\field\Plugin\Type\Widget\WidgetInterface::extractFormValues().
*/
public function submit(EntityInterface $entity, $langcode, array &$items, array $form, array &$form_state) {
public function extractFormValues(EntityInterface $entity, $langcode, array &$items, array $form, array &$form_state) {
$field_name = $this->field['field_name'];
// Extract the values from $form_state['values'].
......
......@@ -63,7 +63,7 @@ public function form(EntityInterface $entity, $langcode, array $items, array &$f
* @param array $form_state
* The form state.
*/
public function submit(EntityInterface $entity, $langcode, array &$items, array $form, array &$form_state);
public function extractFormValues(EntityInterface $entity, $langcode, array &$items, array $form, array &$form_state);
/**
* Reports field-level validation errors against actual form elements.
......
......@@ -460,9 +460,9 @@ function testFieldAttachForm() {
}
/**
* Test field_attach_submit().
* Test field_attach_extract_form_values().
*/
function testFieldAttachSubmit() {
function testFieldAttachExtractFormValues() {
$this->createFieldWithInstance('_2');
$entity_type = 'test_entity';
......@@ -510,9 +510,9 @@ function testFieldAttachSubmit() {
$form_state['values'][$this->field_name][$langcode] = $values;
$form_state['values'][$this->field_name_2][$langcode] = $values_2;
// Call field_attach_submit() for all fields.
// Call field_attach_extract_form_values() for all fields.
$entity = clone($entity_init);
field_attach_submit($entity, $form, $form_state);
field_attach_extract_form_values($entity, $form, $form_state);
asort($weights);
asort($weights_2);
......@@ -531,10 +531,10 @@ function testFieldAttachSubmit() {
}
$this->assertIdentical($entity->{$this->field_name_2}[$langcode], $expected_values_2, 'Submit filters empty values');
// Call field_attach_submit() for a single field (the second field).
// Call field_attach_extract_form_values() for a single field (the second field).
$options = array('field_name' => $this->field_name_2);
$entity = clone($entity_init);
field_attach_submit($entity, $form, $form_state, $options);
field_attach_extract_form_values($entity, $form, $form_state, $options);
$expected_values_2 = array();
foreach ($weights_2 as $key => $value) {
if ($key != 1) {
......
......@@ -253,11 +253,11 @@ function field_test_entity_nested_form($form, &$form_state, $entity_1, $entity_2
*/
function field_test_entity_nested_form_validate($form, &$form_state) {
$entity_1 = entity_create('test_entity', $form_state['values']);
field_attach_submit($entity_1, $form, $form_state);
field_attach_extract_form_values($entity_1, $form, $form_state);
field_attach_form_validate($entity_1, $form, $form_state);
$entity_2 = entity_create('test_entity', $form_state['values']['entity_2']);
field_attach_submit($entity_2, $form['entity_2'], $form_state);
field_attach_extract_form_values($entity_2, $form['entity_2'], $form_state);
field_attach_form_validate($entity_2, $form['entity_2'], $form_state);
}
......@@ -266,11 +266,11 @@ function field_test_entity_nested_form_validate($form, &$form_state) {
*/
function field_test_entity_nested_form_submit($form, &$form_state) {
$entity_1 = entity_create('test_entity', $form_state['values']);
field_attach_submit($entity_1, $form, $form_state);
field_attach_extract_form_values($entity_1, $form, $form_state);
field_test_entity_save($entity_1);
$entity_2 = entity_create('test_entity', $form_state['values']['entity_2']);
field_attach_submit($entity_2, $form['entity_2'], $form_state);
field_attach_extract_form_values($entity_2, $form['entity_2'], $form_state);
field_test_entity_save($entity_2);
drupal_set_message(t('test_entities @id_1 and @id_2 have been updated.', array('@id_1' => $entity_1->ftid, '@id_2' => $entity_2->ftid)));
......
......@@ -971,7 +971,7 @@ function field_ui_field_edit_form_validate($form, &$form_state) {
// Extract the 'default value'.
$items = array();
$instance->getWidget()->submit($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state);
$instance->getWidget()->extractFormValues($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state);
// Grab the field definition from $form_state.
$field_state = field_form_get_state($element['#parents'], $field_name, LANGUAGE_NOT_SPECIFIED, $form_state);
......@@ -1012,7 +1012,7 @@ function field_ui_field_edit_form_submit($form, &$form_state) {
// Extract field values.
$items = array();
$instance->getWidget()->submit($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state);
$instance->getWidget()->extractFormValues($entity, LANGUAGE_NOT_SPECIFIED, $items, $element, $form_state);
$instance['default_value'] = $items ? $items : NULL;
}
......
......@@ -820,8 +820,8 @@ function hook_node_validate(Drupal\node\Node $node, $form, &$form_state) {
* object, but before the node is saved or previewed. It is a chance for modules
* to adjust the node's properties from what they are simply after a copy from
* $form_state['values']. This hook is intended for adjusting non-field-related
* properties. See hook_field_attach_submit() for customizing field-related
* properties.
* properties. See hook_field_attach_extract_form_values() for customizing
* field-related properties.
*
* @param Drupal\node\Node $node
* The node entity being updated in response to a form submission.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment