You are here

public function WeightSelector::viewsFormSubmit in Weight 8.3

File

src/Plugin/views/field/WeightSelector.php, line 132

Class

WeightSelector
Field handler to present a weight selector element.

Namespace

Drupal\weight\Plugin\views\field

Code

public function viewsFormSubmit(array &$form, FormStateInterface $form_state) {
  $field_name = $form_state
    ->getValue('views_field');
  if (!$field_name) {
    return;
  }
  $rows = $form_state
    ->getValue($field_name);
  foreach ($rows as $row) {
    if ($row['langcode']) {
      $entity = $row['entity']
        ->getTranslation($row['langcode']);
    }
    else {
      $entity = $row['entity'];
    }
    if ($entity && $entity
      ->hasField($field_name)) {
      $entity
        ->set($field_name, $row['weight']);
      $entity
        ->save();
      \Drupal::messenger()
        ->addMessage($this
        ->t('Your changes have been saved.'));
    }
  }
}