You are here

trait ViewsBulkOperationsFormTrait in Views Bulk Operations (VBO) 8.2

Same name and namespace in other branches
  1. 8.3 src/Form/ViewsBulkOperationsFormTrait.php \Drupal\views_bulk_operations\Form\ViewsBulkOperationsFormTrait
  2. 8 src/Form/ViewsBulkOperationsFormTrait.php \Drupal\views_bulk_operations\Form\ViewsBulkOperationsFormTrait
  3. 4.0.x src/Form/ViewsBulkOperationsFormTrait.php \Drupal\views_bulk_operations\Form\ViewsBulkOperationsFormTrait

Defines common methods for Views Bulk Operations forms.

Hierarchy

2 files declare their use of ViewsBulkOperationsFormTrait
ViewsBulkOperationsBulkForm.php in src/Plugin/views/field/ViewsBulkOperationsBulkForm.php
ViewsBulkOperationsController.php in src/Controller/ViewsBulkOperationsController.php

File

src/Form/ViewsBulkOperationsFormTrait.php, line 11

Namespace

Drupal\views_bulk_operations\Form
View source
trait ViewsBulkOperationsFormTrait {

  /**
   * The tempstore object associated with the current view.
   *
   * @var \Drupal\user\PrivateTempStore
   */
  protected $viewTempstore;

  /**
   * The tempstore name.
   *
   * @var string
   */
  protected $tempStoreName;

  /**
   * Helper function to prepare data needed for proper form display.
   *
   * @param string $view_id
   *   The current view ID.
   * @param string $display_id
   *   The current view display ID.
   *
   * @return array
   *   Array containing data for the form builder.
   */
  protected function getFormData($view_id, $display_id) {

    // Get tempstore data.
    $form_data = $this
      ->getTempstoreData($view_id, $display_id);

    // Get data needed for selected entities list.
    if (!empty($form_data['list'])) {
      $form_data['entity_labels'] = [];
      $form_data['selected_count'] = 0;
      foreach ($form_data['list'] as $item) {
        $form_data['selected_count']++;
        $form_data['entity_labels'][] = $item[4];
      }
    }
    elseif ($form_data['total_results']) {
      $form_data['selected_count'] = $form_data['total_results'];
    }
    else {
      $form_data['selected_count'] = (string) $this
        ->t('all');
    }
    return $form_data;
  }

  /**
   * Calculates the bulk form key for an entity.
   *
   * This generates a key that is used as the checkbox return value when
   * submitting the bulk form.
   *
   * @param \Drupal\Core\Entity\EntityInterface $entity
   *   The entity to calculate a bulk form key for.
   * @param mixed $base_field_value
   *   The value of the base field for this view result.
   *
   * @return string
   *   The bulk form key representing the entity id, language and revision (if
   *   applicable) as one string.
   *
   * @see self::loadEntityFromBulkFormKey()
   */
  public static function calculateEntityBulkFormKey(EntityInterface $entity, $base_field_value) {

    // We don't really need the entity ID or type ID, since only the
    // base field value and language are used to select rows, but
    // other modules may need those values.
    $key_parts = [
      $base_field_value,
      $entity
        ->language()
        ->getId(),
      $entity
        ->getEntityTypeId(),
      $entity
        ->id(),
    ];

    // An entity ID could be an arbitrary string (although they are typically
    // numeric). JSON then Base64 encoding ensures the bulk_form_key is
    // safe to use in HTML, and that the key parts can be retrieved.
    $key = json_encode($key_parts);
    return base64_encode($key);
  }

  /**
   * Get an entity list item from a bulk form key and label.
   *
   * @param string $bulkFormKey
   *   A bulk form key.
   * @param mixed $label
   *   Entity label, string or
   *   \Drupal\Core\StringTranslation\TranslatableMarkup.
   *
   * @return array
   *   Entity list item.
   */
  protected function getListItem($bulkFormKey, $label) {
    $item = json_decode(base64_decode($bulkFormKey));
    $item[] = $label;
    return $item;
  }

  /**
   * Initialize the current view tempstore object.
   */
  protected function getTempstore($view_id = NULL, $display_id = NULL) {
    if (!isset($this->viewTempstore)) {
      $this->tempStoreName = 'views_bulk_operations_' . $view_id . '_' . $display_id;
      $this->viewTempstore = $this->tempStoreFactory
        ->get($this->tempStoreName);
    }
    return $this->viewTempstore;
  }

  /**
   * Gets the current view user tempstore data.
   *
   * @param string $view_id
   *   The current view ID.
   * @param string $display_id
   *   The display ID of the current view.
   */
  protected function getTempstoreData($view_id = NULL, $display_id = NULL) {
    $data = $this
      ->getTempstore($view_id, $display_id)
      ->get($this
      ->currentUser()
      ->id());
    return $data;
  }

  /**
   * Sets the current view user tempstore data.
   *
   * @param array $data
   *   The data to set.
   * @param string $view_id
   *   The current view ID.
   * @param string $display_id
   *   The display ID of the current view.
   */
  protected function setTempstoreData(array $data, $view_id = NULL, $display_id = NULL) {
    return $this
      ->getTempstore($view_id, $display_id)
      ->set($this
      ->currentUser()
      ->id(), $data);
  }

  /**
   * Deletes the current view user tempstore data.
   *
   * @param string $view_id
   *   The current view ID.
   * @param string $display_id
   *   The display ID of the current view.
   */
  protected function deleteTempstoreData($view_id = NULL, $display_id = NULL) {
    return $this
      ->getTempstore($view_id, $display_id)
      ->delete($this
      ->currentUser()
      ->id());
  }

  /**
   * Add a cancel button into a VBO form.
   *
   * @param array $form
   *   The form definition.
   */
  protected function addCancelButton(array &$form) {
    $form['actions']['cancel'] = [
      '#type' => 'submit',
      '#value' => $this
        ->t('Cancel'),
      '#submit' => [
        [
          $this,
          'cancelForm',
        ],
      ],
      '#limit_validation_errors' => [],
    ];
  }

  /**
   * Submit callback to cancel an action and return to the view.
   *
   * @param array $form
   *   The form definition.
   * @param \Drupal\Core\Form\FormStateInterface $form_state
   *   The form state.
   */
  public function cancelForm(array &$form, FormStateInterface $form_state) {
    $form_data = $form_state
      ->get('views_bulk_operations');
    drupal_set_message($this
      ->t('Canceled "%action".', [
      '%action' => $form_data['action_label'],
    ]));
    $form_state
      ->setRedirectUrl($form_data['redirect_url']);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ViewsBulkOperationsFormTrait::$tempStoreName protected property The tempstore name.
ViewsBulkOperationsFormTrait::$viewTempstore protected property The tempstore object associated with the current view.
ViewsBulkOperationsFormTrait::addCancelButton protected function Add a cancel button into a VBO form.
ViewsBulkOperationsFormTrait::calculateEntityBulkFormKey public static function Calculates the bulk form key for an entity.
ViewsBulkOperationsFormTrait::cancelForm public function Submit callback to cancel an action and return to the view.
ViewsBulkOperationsFormTrait::deleteTempstoreData protected function Deletes the current view user tempstore data.
ViewsBulkOperationsFormTrait::getFormData protected function Helper function to prepare data needed for proper form display.
ViewsBulkOperationsFormTrait::getListItem protected function Get an entity list item from a bulk form key and label.
ViewsBulkOperationsFormTrait::getTempstore protected function Initialize the current view tempstore object.
ViewsBulkOperationsFormTrait::getTempstoreData protected function Gets the current view user tempstore data.
ViewsBulkOperationsFormTrait::setTempstoreData protected function Sets the current view user tempstore data.