You are here

public function BulkEditFormTrait::execute in Views Bulk Edit 8.2

1 call to BulkEditFormTrait::execute()
BulkEditForm::submitForm in src/Form/BulkEditForm.php
Form submission handler.

File

src/Form/BulkEditFormTrait.php, line 411

Class

BulkEditFormTrait
Common methods for Views Bulk Edit forms.

Namespace

Drupal\views_bulk_edit\Form

Code

public function execute($entity = NULL) {

  /** @var \Drupal\Core\Entity\ContentEntityInterface $entity */
  $type_id = $entity
    ->getEntityTypeId();
  $bundle = $entity
    ->bundle();
  $result = $this
    ->t('No values changed');

  // Load the edit revision for safe editing.
  $entity = $this->entityRepository
    ->getActive($type_id, $entity
    ->id());
  if (isset($this->configuration[$type_id][$bundle])) {
    $values = $this->configuration[$type_id][$bundle]['values'];
    $change_method = $this->configuration[$type_id][$bundle]['change_method'];
    foreach ($values as $field => $value) {
      if (isset($change_method[$field])) {
        switch ($change_method[$field]) {
          case 'new':
            $current_value = $entity->{$field}
              ->getValue();
            $value = array_unique(array_merge($current_value, $value), SORT_REGULAR);
            break;
          case 'append':
            $current_value = $entity->{$field}
              ->getValue();
            if ($current_value) {
              $value[0]['value'] = $current_value[0]['value'] . ' ' . $value[0]['value'];
            }
            break;
        }
      }
      $entity->{$field}
        ->setValue($value);
    }

    // Set up revision defaults if entity is revisionable.
    if (!empty($this->configuration[$type_id][$bundle]['revision_information']['revision'])) {
      $entity
        ->setNewRevision();
      $entity
        ->setRevisionCreationTime($this->time
        ->getCurrentTime());
      $entity
        ->setRevisionUserId($this->currentUser
        ->id());
      if (empty($this->configuration[$type_id][$bundle]['revision_information']['revision_log'])) {
        $entity
          ->setRevisionLogMessage($this
          ->formatPlural(count($values), 'Edited as a part of bulk operation. Field changed: @fields', 'Edited as a part of bulk operation. Fields changed: @fields', [
          '@fields' => implode(', ', array_keys($values)),
        ]));
      }
      else {
        $entity
          ->setRevisionLogMessage($this->configuration[$type_id][$bundle]['revision_information']['revision_log']);
      }
    }
    $entity
      ->save();
    $result = $this
      ->t('Modify field values');
  }
  return $result;
}