You are here

protected function ModuleHooksForm::copyFormValuesToEntity in Module Builder 8.3

Copies top-level form values to entity properties

This should not change existing entity properties that are not being edited by this form.

Parameters

\Drupal\Core\Entity\EntityInterface $entity: The entity the current form should operate upon.

array $form: A nested array of form elements comprising the form.

\Drupal\Core\Form\FormStateInterface $form_state: The current state of the form.

Overrides ComponentSectionForm::copyFormValuesToEntity

File

src/Form/ModuleHooksForm.php, line 135

Class

ModuleHooksForm
Form for selecting hooks.

Namespace

Drupal\module_builder\Form

Code

protected function copyFormValuesToEntity(EntityInterface $entity, array $form, FormStateInterface $form_state) {
  $values = $form_state
    ->getValues();

  // We can't just iterate over $values, because of a core bug with
  // EntityForm, so we need to know which keys to look at.
  // See https://www.drupal.org/node/2665714.
  $groups = $form_state
    ->get('module_builder_groups');
  $hooks = [];
  foreach ($groups as $group) {
    $group_values = $values[$group . '_hooks'];

    // Filter out empty values. (FormAPI *still* doesn't do this???)
    $group_hooks = array_filter($group_values);

    // Store as a numeric array.
    $group_hooks = array_keys($group_hooks);
    $hooks = array_merge($group_hooks, $hooks);
  }
  $data = $entity
    ->get('data');
  $data['hooks'] = $hooks;
  $entity
    ->set('data', $data);
}