You are here

public function RateWidgetForm::save in Rate 8.2

Form submission handler for the 'save' action.

Normally this method should be overridden to provide specific messages to the user and redirect the form after the entity has been saved.

Parameters

array $form: An associative array containing the structure of the form.

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

Return value

int Either SAVED_NEW or SAVED_UPDATED, depending on the operation performed.

Overrides EntityForm::save

File

src/RateWidgetForm.php, line 646

Class

RateWidgetForm
Form controller for vote type forms.

Namespace

Drupal\rate

Code

public function save(array $form, FormStateInterface $form_state) {
  $rate_widget = $this->entity;
  $rate_widget
    ->set('id', trim($rate_widget
    ->id()));
  $rate_widget
    ->set('label', trim($rate_widget
    ->label()));
  $rate_widget
    ->set('template', $rate_widget
    ->get('template'));
  $rate_widget
    ->set('value_type', $rate_widget
    ->get('value_type'));

  // Prepare the options for saving.
  $options = $rate_widget
    ->get('options');
  unset($options['actions']);
  $options = $options['table'];

  // Remove empty optons.
  foreach ($options as $key => $value) {
    if ($options[$key]['value'] == NULL) {
      unset($options[$key]);
    }
  }

  // Reindex and set the options.
  $options = array_values($options);
  $rate_widget
    ->set('options', $options);

  // Prepare the entities for saving.
  $entities = $rate_widget
    ->get('entities');
  $entities = $entities['table'];

  // Remove empty entities.
  foreach ($entities as $key => $value) {
    if ($entities[$key]['entity_enabled'] == NULL && $entities[$key]['comment_enabled'] == NULL) {
      unset($entities[$key]);
    }
  }

  // Reindex and set the entities.
  $entities = array_values($entities);
  $entity_types = [];
  $comment_types = [];

  // Save current entities to remove rate_vote_deadline field.
  $current_entities = $rate_widget
    ->get('entity_types');
  $rate_widget
    ->set('entities', $entities);

  // Split the values in separate arrays for compatibility with D7.
  foreach ($entities as $key => $value) {
    if ($value['entity_enabled'] && $value['comment_enabled']) {
      $entity_types[] = $value['entity_enabled'];
      $comment_types[] = $value['comment_enabled'];
    }
    elseif ($value['entity_enabled']) {
      $entity_types[] = $value['entity_enabled'];
    }
    elseif ($value['comment_enabled']) {
      $comment_types[] = $value['comment_enabled'];
    }
  }
  $rate_widget
    ->set('entity_types', $entity_types);
  $rate_widget
    ->set('comment_types', $comment_types);

  // Remove deadline field if rate widget was detached or deadline unchecked.
  $removed_entities = array_diff($current_entities, $rate_widget
    ->get('entity_types'));
  $deadline_value = $form['voting']['use_deadline']['#default_value'];
  $deadline_new_value = $form['voting']['use_deadline']['#value'];
  if ($deadline_new_value == 0 && $deadline_new_value != $deadline_value) {
    $remove_deadline = TRUE;
  }
  $field_name = 'field_rate_vote_deadline';

  // Remove the deadline when the use deadline checkbox is being unset.
  if (isset($remove_deadline) && !empty($current_entities)) {
    foreach ($current_entities as $key => $entity) {
      $parameter = explode('.', $entity);
      $field_config = FieldConfig::loadByName($parameter[0], $parameter[1], $field_name);
      if (!empty($field_config)) {
        $field_config
          ->delete();
        $this->messenger
          ->addStatus($this
          ->t('Field %field removed from %entity.', [
          '%field' => 'Rate vote deadline',
          '%entity' => $entity,
        ]));
      }
    }
  }

  // Remove the deadline when the entity list of the widget is changed.
  if ($deadline_new_value == 1 && !isset($remove_deadline) && !empty($removed_entities)) {
    foreach ($removed_entities as $key => $entity) {
      $parameter = explode('.', $entity);
      $field_config = FieldConfig::loadByName($parameter[0], $parameter[1], $field_name);
      if (!empty($field_config)) {
        $field_config
          ->delete();
        $this->messenger
          ->addStatus($this
          ->t('Field %field removed from %entity.', [
          '%field' => 'Rate vote deadline',
          '%entity' => $entity,
        ]));
      }
    }
  }

  // Set the voting, display and results settings.
  $voting = $rate_widget
    ->get('voting') ? $rate_widget
    ->get('voting') : [];
  $display = $rate_widget
    ->get('display') ? $rate_widget
    ->get('display') : [];
  $results = $rate_widget
    ->get('results') ? $rate_widget
    ->get('results') : [];
  $rate_widget
    ->set('voting', $voting);
  $rate_widget
    ->set('display', $display);
  $rate_widget
    ->set('results', $results);

  // Save the widget.
  $status = $rate_widget
    ->save();
  $t_args = [
    '%name' => $rate_widget
      ->label(),
  ];
  if ($status == SAVED_UPDATED) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('The rate widget %name has been updated.', $t_args));
  }
  elseif ($status == SAVED_NEW) {
    $this
      ->messenger()
      ->addStatus($this
      ->t('The rate widget %name has been added.', $t_args));
    $context = array_merge($t_args, [
      'link' => $rate_widget
        ->toLink($this
        ->t('View'), 'collection')
        ->toString(),
    ]);
    $this
      ->logger('rate_widget')
      ->notice('Added rate widget %name.', $context);
  }
  $this->entityFieldManager
    ->clearCachedFieldDefinitions();
  $form_state
    ->setRedirect('entity.rate_widget.collection');
}