Commit 074fdb1f authored by alexpott's avatar alexpott

Issue #2497847 by yched, amateescu: Simplify EntityDisplayEditFormBase ajax...

Issue #2497847 by yched, amateescu: Simplify EntityDisplayEditFormBase ajax rebuild flow to work only with $this->entity
parent d4047abb
...@@ -238,9 +238,9 @@ public function form(array $form, FormStateInterface $form_state) { ...@@ -238,9 +238,9 @@ public function form(array $form, FormStateInterface $form_state) {
'#type' => 'submit', '#type' => 'submit',
'#value' => $this->t('Refresh'), '#value' => $this->t('Refresh'),
'#op' => 'refresh_table', '#op' => 'refresh_table',
'#submit' => array(array($this, 'multistepSubmit')), '#submit' => array('::multistepSubmit'),
'#ajax' => array( '#ajax' => array(
'callback' => array($this, 'multistepAjax'), 'callback' => '::multistepAjax',
'wrapper' => 'field-display-overview-wrapper', 'wrapper' => 'field-display-overview-wrapper',
'effect' => 'fade', 'effect' => 'fade',
// The button stays hidden, so we hide the Ajax spinner too. Ad-hoc // The button stays hidden, so we hide the Ajax spinner too. Ad-hoc
...@@ -316,7 +316,6 @@ protected function buildFieldRow(FieldDefinitionInterface $field_definition, arr ...@@ -316,7 +316,6 @@ protected function buildFieldRow(FieldDefinitionInterface $field_definition, arr
'#attributes' => array('class' => array('field-name')), '#attributes' => array('class' => array('field-name')),
), ),
), ),
); );
$field_row['plugin'] = array( $field_row['plugin'] = array(
...@@ -332,27 +331,14 @@ protected function buildFieldRow(FieldDefinitionInterface $field_definition, arr ...@@ -332,27 +331,14 @@ protected function buildFieldRow(FieldDefinitionInterface $field_definition, arr
'settings_edit_form' => array(), 'settings_edit_form' => array(),
); );
// Check the currently selected plugin, and merge persisted values for its
// settings.
if ($display_type = $form_state->getValue(array('fields', $field_name, 'type'))) {
$display_options['type'] = $display_type;
}
$plugin_settings = $form_state->get('plugin_settings');
if (isset($plugin_settings[$field_name]['settings'])) {
$display_options['settings'] = $plugin_settings[$field_name]['settings'];
}
if (isset($plugin_settings[$field_name]['third_party_settings'])) {
$display_options['third_party_settings'] = $plugin_settings[$field_name]['third_party_settings'];
}
// Get the corresponding plugin object. // Get the corresponding plugin object.
$plugin = $this->getPlugin($field_definition, $display_options); $plugin = $this->entity->getRenderer($field_name);
// Base button element for the various plugin settings actions. // Base button element for the various plugin settings actions.
$base_button = array( $base_button = array(
'#submit' => array(array($this, 'multistepSubmit')), '#submit' => array('::multistepSubmit'),
'#ajax' => array( '#ajax' => array(
'callback' => array($this, 'multistepAjax'), 'callback' => '::multistepAjax',
'wrapper' => 'field-display-overview-wrapper', 'wrapper' => 'field-display-overview-wrapper',
'effect' => 'fade', 'effect' => 'fade',
), ),
...@@ -517,6 +503,13 @@ protected function buildExtraFieldRow($field_id, $extra_field) { ...@@ -517,6 +503,13 @@ protected function buildExtraFieldRow($field_id, $extra_field) {
* {@inheritdoc} * {@inheritdoc}
*/ */
public function submitForm(array &$form, FormStateInterface $form_state) { public function submitForm(array &$form, FormStateInterface $form_state) {
// If the main "Save" button was submitted while a field settings subform
// was being edited, update the new incoming settings when rebuilding the
// entity, just as if the subform's "Update" button had been submitted.
if ($edit_field = $form_state->get('plugin_settings_edit')) {
$form_state->set('plugin_settings_update', $edit_field);
}
parent::submitForm($form, $form_state); parent::submitForm($form, $form_state);
$form_values = $form_state->getValues(); $form_values = $form_state->getValues();
...@@ -562,57 +555,30 @@ protected function copyFormValuesToEntity(EntityInterface $entity, array $form, ...@@ -562,57 +555,30 @@ protected function copyFormValuesToEntity(EntityInterface $entity, array $form,
// Collect data for 'regular' fields. // Collect data for 'regular' fields.
foreach ($form['#fields'] as $field_name) { foreach ($form['#fields'] as $field_name) {
// Retrieve the stored field settings to merge with the incoming
// values.
$values = $form_values['fields'][$field_name]; $values = $form_values['fields'][$field_name];
if ($values['type'] == 'hidden') { if ($values['type'] == 'hidden') {
$entity->removeComponent($field_name); $entity->removeComponent($field_name);
} }
else { else {
// Get plugin settings. They lie either directly in submitted form $options = $entity->getComponent($field_name);
// values (if the whole form was submitted while some plugin settings
// were being edited), or have been persisted in $form_state. // Update field settings only if the submit handler told us to.
$plugin_settings = $form_state->get('plugin_settings'); if ($form_state->get('plugin_settings_update') === $field_name) {
$settings = array(); // Only store settings actually used by the selected plugin.
if (isset($values['settings_edit_form']['settings'])) { $default_settings = $this->pluginManager->getDefaultSettings($options['type']);
$settings = $values['settings_edit_form']['settings']; $options['settings'] = array_intersect_key($values['settings_edit_form']['settings'], $default_settings);
$options['third_party_settings'] = isset($values['settings_edit_form']['third_party_settings']) ? $values['settings_edit_form']['third_party_settings'] : [];
$form_state->set('plugin_settings_update', NULL);
} }
elseif (isset($plugin_settings[$field_name]['settings'])) {
$settings = $plugin_settings[$field_name]['settings'];
}
elseif ($current_options = $entity->getComponent($field_name)) {
$settings = $current_options['settings'];
}
$third_party_settings = array();
if (isset($values['settings_edit_form']['third_party_settings'])) {
$third_party_settings = $values['settings_edit_form']['third_party_settings'];
}
elseif (isset($plugin_settings[$field_name]['third_party_settings'])) {
$third_party_settings = $plugin_settings[$field_name]['third_party_settings'];
}
elseif (($current_options = $entity->getComponent($field_name)) && isset($current_options['third_party_settings'])) {
$third_party_settings = $current_options['third_party_settings'];
}
// Only save settings actually used by the selected plugin.
$default_settings = $this->pluginManager->getDefaultSettings($values['type']);
$settings = array_intersect_key($settings, $default_settings);
// Default component values.
$component_values = array(
'type' => $values['type'],
'weight' => $values['weight'],
'settings' => $settings,
'third_party_settings' => $third_party_settings,
);
$options['type'] = $values['type'];
$options['weight'] = $values['weight'];
// Only formatters have configurable label visibility. // Only formatters have configurable label visibility.
if (isset($values['label'])) { if (isset($values['label'])) {
$component_values['label'] = $values['label']; $options['label'] = $values['label'];
} }
$entity->setComponent($field_name, $options);
$entity->setComponent($field_name, $component_values);
} }
} }
...@@ -644,15 +610,12 @@ public function multistepSubmit($form, FormStateInterface $form_state) { ...@@ -644,15 +610,12 @@ public function multistepSubmit($form, FormStateInterface $form_state) {
break; break;
case 'update': case 'update':
// Store the saved settings, and set the field back to 'non edit' mode. // Set the field back to 'non edit' mode, and update $this->entity with
// the new settings fro the next rebuild.
$field_name = $trigger['#field_name']; $field_name = $trigger['#field_name'];
if ($plugin_settings = $form_state->getValue(array('fields', $field_name, 'settings_edit_form', 'settings'))) {
$form_state->set(['plugin_settings', $field_name, 'settings'], $plugin_settings);
}
if ($plugin_third_party_settings = $form_state->getValue(array('fields', $field_name, 'settings_edit_form', 'third_party_settings'))) {
$form_state->set(['plugin_settings', $field_name, 'third_party_settings'], $plugin_third_party_settings);
}
$form_state->set('plugin_settings_edit', NULL); $form_state->set('plugin_settings_edit', NULL);
$form_state->set('plugin_settings_update', $field_name);
$this->entity = $this->buildEntity($form, $form_state);
break; break;
case 'cancel': case 'cancel':
...@@ -666,7 +629,6 @@ public function multistepSubmit($form, FormStateInterface $form_state) { ...@@ -666,7 +629,6 @@ public function multistepSubmit($form, FormStateInterface $form_state) {
$updated_rows = explode(' ', $form_state->getValue('refresh_rows')); $updated_rows = explode(' ', $form_state->getValue('refresh_rows'));
$plugin_settings_edit = $form_state->get('plugin_settings_edit'); $plugin_settings_edit = $form_state->get('plugin_settings_edit');
if ($plugin_settings_edit && in_array($plugin_settings_edit, $updated_rows)) { if ($plugin_settings_edit && in_array($plugin_settings_edit, $updated_rows)) {
$form_state->set('plugin_settings_edit', NULL); $form_state->set('plugin_settings_edit', NULL);
} }
break; break;
...@@ -856,19 +818,6 @@ protected function getExtraFields() { ...@@ -856,19 +818,6 @@ protected function getExtraFields() {
*/ */
abstract protected function getEntityDisplay($entity_type_id, $bundle, $mode); abstract protected function getEntityDisplay($entity_type_id, $bundle, $mode);
/**
* Returns the widget or formatter plugin for a field.
*
* @param \Drupal\Core\Field\FieldDefinitionInterface $field_definition
* The field.
* @param array $configuration
* The plugin configuration
*
* @return object
* The corresponding plugin.
*/
abstract protected function getPlugin(FieldDefinitionInterface $field_definition, $configuration);
/** /**
* Returns an array of widget or formatter options for a field. * Returns an array of widget or formatter options for a field.
* *
......
...@@ -59,23 +59,6 @@ protected function getEntityDisplay($entity_type_id, $bundle, $mode) { ...@@ -59,23 +59,6 @@ protected function getEntityDisplay($entity_type_id, $bundle, $mode) {
return entity_get_form_display($entity_type_id, $bundle, $mode); return entity_get_form_display($entity_type_id, $bundle, $mode);
} }
/**
* {@inheritdoc}
*/
protected function getPlugin(FieldDefinitionInterface $field_definition, $configuration) {
$plugin = NULL;
if ($configuration && $configuration['type'] != 'hidden') {
$plugin = $this->pluginManager->getInstance(array(
'field_definition' => $field_definition,
'form_mode' => $this->entity->getMode(),
'configuration' => $configuration
));
}
return $plugin;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
...@@ -92,23 +92,6 @@ protected function getEntityDisplay($entity_type_id, $bundle, $mode) { ...@@ -92,23 +92,6 @@ protected function getEntityDisplay($entity_type_id, $bundle, $mode) {
return entity_get_display($entity_type_id, $bundle, $mode); return entity_get_display($entity_type_id, $bundle, $mode);
} }
/**
* {@inheritdoc}
*/
protected function getPlugin(FieldDefinitionInterface $field_definition, $configuration) {
$plugin = NULL;
if ($configuration && $configuration['type'] != 'hidden') {
$plugin = $this->pluginManager->getInstance(array(
'field_definition' => $field_definition,
'view_mode' => $this->entity->getMode(),
'configuration' => $configuration
));
}
return $plugin;
}
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
......
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