You are here

public static function ComponentSectionForm::updateVariantValidate in Module Builder 8.3

Validate handler for the update variant button.

This removes variant-dependent values if the variant has changed.

File

src/Form/ComponentSectionForm.php, line 725

Class

ComponentSectionForm
Generic form for entering a section of data for a component.

Namespace

Drupal\module_builder\Form

Code

public static function updateVariantValidate(array $form, FormStateInterface $form_state) {
  $button = $form_state
    ->getTriggeringElement();

  // WTF FormAPI?
  // Why does this
  if (array_pop($button['#parents']) != ':update_variant') {
    return;
  }

  // Get the value of the mutable data variant property.
  $values_address = $button['#parents'];
  $values_address[] = $button['#variant_data_name'];
  $values = $form_state
    ->getValues();
  $submitted_variant_value = NestedArray::getValue($values, $values_address);

  // Checking the variant property form element is #required hasn't happened
  // yet at this point. So bail and let FormAPI set a form error.
  if (empty($submitted_variant_value)) {
    return;
  }

  // Get the containing variant data item, using the address set in the
  // button.
  $data = $form_state
    ->get('data');
  $variant_data_item = $data
    ->getItem($button['#data_address']);

  // Clean up the values if the current submission is changing the variant
  // property.
  // We can't determine whether this is happening by comparing the form
  // state's variant value with the data item's variant value, because
  // validateForm()'s call to CleanUpValues() has already set the values on
  // the data item, and then set that back in the form state. Which is ugly,
  // because it means a button validator such as this one doesn't have a
  // proper picture of what is going on. TODO: look at core entity forms and
  // see whether they set the entity back on the form after building it in
  // validation. A quick look suggests that they don't.
  $variant_properties = $variant_data_item
    ->getParent()
    ->getProperties();
  $complex_values_address = $button['#parents'];
  $complex_values = NestedArray::getValue($values, $complex_values_address);
  $cleaned_complex_values = array_intersect_key($complex_values, $variant_properties);
  NestedArray::setValue($values, $complex_values_address, $cleaned_complex_values);
  $form_state
    ->setValues($values);

  // TODO: this bit WOULD work if validateForm() weren't updating the data
  // item stored in the form state. As it stands, it does nothing because
  // the two values will be equal even when the variant is being changed.
  if ($submitted_variant_value != $variant_data_item->value) {
    $complex_values_address = $button['#parents'];
    $complex_values = NestedArray::getValue($values, $complex_values_address);
    $complex_values = array_intersect_key($complex_values, [
      $button['#variant_data_name'] => TRUE,
    ]);
    NestedArray::setValue($values, $complex_values_address, $complex_values);
    $form_state
      ->setValues($values);
  }
}