You are here

class GridActionForm in Filebrowser 8.2

Same name and namespace in other branches
  1. 3.x src/Form/GridActionForm.php \Drupal\filebrowser\Form\GridActionForm

Class GridActionForm.

@package Drupal\filebrowser\Form

Hierarchy

Expanded class hierarchy of GridActionForm

File

src/Form/GridActionForm.php, line 19

Namespace

Drupal\filebrowser\Form
View source
class GridActionForm extends FormBase {

  /**
   * @var string
   * The relative root for this file listing.
   * The complete location of a file is determined by
   * folder_path + relativeRoot = path:
   * local//: path_to_folderpath/folder1/folder2/file.txt
   * In this example "folder1/folder2" is the relativeRoot
   */
  protected $relativeRoot;

  /**
   * @var array
   * Array of allowed actions by this user on this node.
   * Used to build form buttons and link. Array is created by
   * Common::allowedActions
   */
  protected $allowedActions;

  /**
   * @var int
   * fid of the relative root
   */
  protected $relativeFid;

  /**
   * @var string
   *String containing Id's of the selected files
   */
  protected $fids;

  /**
   * Node holding this form
   * @var NodeInterface $node
   */
  protected $node;

  /**
   * @var integer $nid
   */
  protected $nid;

  /**
   * {@inheritdoc}
   */

  /**
   * @var \Drupal\filebrowser\Services\Common
   */
  protected $common;

  /**
   * @var FormHelper
   */
  protected $helper;

  /**
   * @var boolean $error
   * Set to true when no items are selected on the form
   */
  protected $error;
  public function getFormId() {
    return 'grid_action_form';
  }

  /**
   * @param array $params
   * Required data to build the for:
   * Associative array keyed 'actions', 'node', 'data'
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $items = null, $params = null) {
    $this->common = \Drupal::service('filebrowser.common');
    $this->helper = \Drupal::service('form.helper');
    $this->node = $params['node'];
    $this->nid = $this->node
      ->id();
    $this->relativeFid = empty($params['data']['fid']) ? 0 : $params['data']['fid'];
    $form = [];
    $this->helper
      ->initForm($form, $params['node']);
    $form['#tree'] = TRUE;
    $form['#attached']['library'][] = 'views/views.module';
    $column_width = round(100 / $items['options']['columns']) - 1;

    // Create the form action elements
    // If user has permissions that needs a button or checkbox then has_actions will be true.
    $has_actions = !empty($params['actions']);
    if ($has_actions) {

      // Create the bar containing the action buttons
      $this->helper
        ->createActionBar($form, $params['actions'], $this->relativeFid);

      // Create a "Select all" checkbox
      $form['select_all'] = [
        '#type' => 'checkbox',
        '#title' => $this
          ->t('Select all'),
        '#title_display' => 'before',
        '#ajax' => [
          'callback' => [
            $this,
            'selectAll',
          ],
          'progress' => 'none',
        ],
      ];
    }

    // main container
    $form['container'] = [
      '#type' => 'container',
      '#attributes' => [
        'id' => [
          'filebrowser-checkboxes-container',
        ],
        'class' => [
          'horizontal',
          'clearfix',
        ],
      ],
    ];

    // rows (table-row)
    foreach ($items['grid_items'] as $row_nr => $row) {
      $row_name = 'row_' . $row_nr;
      $form['container'][$row_name] = [
        '#type' => 'container',
        '#options' => '',
        '#attributes' => [
          'class' => [
            'filebrowser-grid-row',
          ],
        ],
      ];

      //columns (td)
      foreach ($row['row'] as $col_nr => $content) {
        $col_name = 'col_' . $col_nr;
        $form['container'][$row_name][$col_name] = [
          '#type' => 'container',
          '#attributes' => [
            'class' => [
              Common::FILEBROWSER_GRID_CONTAINER_COLUMN_CLASS,
            ],
            'width' => $column_width,
          ],
        ];
        if ($has_actions) {
          $form['container'][$row_name][$col_name][$content['content']['file']->fid] = [
            '#type' => 'checkbox',
            '#title' => '',
            '#attributes' => [
              'class' => [
                'filebrowser-checkbox',
              ],
            ],
            '#field_suffix' => $content['content']['grid'],
          ];
        }
        else {
          $form['container'][$row_name][$col_name][$content['content']['file']->fid] = [
            '#markup' => render($content['content']['grid']),
          ];
        }
      }
    }
    return $form;
  }

  /**
   * {@inheritdoc}
   */
  public function validateForm(array &$form, FormStateInterface $form_state) {

    // All submit button needs selected items. If noting selected generate form_error
    $element = $form_state
      ->getTriggeringElement();
    $this->fids = $this
      ->getFids($form, $form_state);
    if (empty($this->fids)) {
      $form_state
        ->setError($element, $this
        ->t('You didn\'t select any item'));
    }
  }
  public function ajaxValidate(&$form, FormStateInterface $form_state) {

    // All submit button needs selected items. If noting selected generate form_error
    $this->fids = $this
      ->getFids($form, $form_state);
    if (empty($this->fids)) {

      //set the error to the submit function:
      \Drupal::logger('filebrowser')
        ->notice('Error no items selected');
      $this->error = true;
    }
  }

  /**
   * {@inheritdoc}
   */
  public function submitForm(array &$form, FormStateInterface $form_state) {

    // No items are selected and we have to display an error. We have to do
    // it here for the ajax enabled buttons. For the normal submit buttons
    // this will be handled by the form validation method.
    if ($this->error) {

      // set the error in the slide down window
      $form_state
        ->setRedirect('filebrowser.no_items_error');
    }
    else {
      $op = $form_state
        ->getTriggeringElement()['#return_value'];
      $route_param = $this->common
        ->routeParam($this->nid, $this->relativeFid);
      $route_param['method'] = 'ajax';
      $route_param['op'] = $op;
      $route_param['fids'] = $this->fids;
      $route = 'filebrowser.action';

      // NB: links are not submitted and do not pass through this submit function
      $form_state
        ->setRedirect($route, $route_param);
    }
  }
  protected function getFids($form, FormStateInterface $form_state) {
    $rows = $form_state
      ->getValue('container');

    // debug($values);
    foreach ($rows as $row) {
      foreach ($row as $item) {
        foreach ($item as $fid => $checked) {
          if ($checked) {
            $fids[] = $fid;
          }
        }
      }
    }
    $selected_fids = empty($fids) ? null : implode(',', $fids);
    return $selected_fids;
  }
  protected function createLink($action) {
    $route = 'filebrowser.action';
    $link_options = [
      'attributes' => [
        'class' => [
          'use-ajax',
          'form-action-link',
          'button',
        ],
      ],
    ];
    return [
      '#markup' => Link::createFromRoute($action['title'], $route, [
        'op' => $action['operation'],
        'method' => 'nojs',
        'nid' => $this->nid,
        'query_fid' => $this->relativeFid,
      ], $link_options)
        ->toString(),
    ];
  }
  public function createButton($action) {
    return [
      '#type' => 'submit',
      '#return_value' => $action['operation'],
      '#value' => $action['title'],
      '#name' => $action['operation'],
      '#title' => $action['title'],
      '#attributes' => [
        'class' => [
          'use-ajax-submit',
        ],
      ],
      '#validate' => [
        [
          $this,
          'ajaxValidate',
        ],
      ],
    ];
  }
  public function createNormalButton($action) {
    return [
      '#type' => 'submit',
      '#return_value' => $action['operation'],
      '#value' => $action['title'],
      '#name' => $action['operation'],
      '#title' => $action['title'],
    ];
  }
  public function selectAll($form, FormStateInterface $form_state) {
    $value = $form_state
      ->getValue('select_all');
    $response = new AjaxResponse();
    $response
      ->addCommand(new InvokeCommand('.filebrowser-checkbox', 'val', [
      [
        'checked',
        $value,
      ],
    ]));
    return $response;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
DependencySerializationTrait::$_entityStorages protected property An array of entity type IDs keyed by the property name of their storages.
DependencySerializationTrait::$_serviceIds protected property An array of service IDs keyed by property name used for serialization.
DependencySerializationTrait::__sleep public function 1
DependencySerializationTrait::__wakeup public function 2
FormBase::$configFactory protected property The config factory. 1
FormBase::$requestStack protected property The request stack. 1
FormBase::$routeMatch protected property The route match.
FormBase::config protected function Retrieves a configuration object.
FormBase::configFactory protected function Gets the config factory for this form. 1
FormBase::container private function Returns the service container.
FormBase::create public static function Instantiates a new instance of this class. Overrides ContainerInjectionInterface::create 87
FormBase::currentUser protected function Gets the current user.
FormBase::getRequest protected function Gets the request object.
FormBase::getRouteMatch protected function Gets the route match.
FormBase::logger protected function Gets the logger for a specific channel.
FormBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
FormBase::resetConfigFactory public function Resets the configuration factory.
FormBase::setConfigFactory public function Sets the config factory for this form.
FormBase::setRequestStack public function Sets the request stack object to use.
GridActionForm::$allowedActions protected property Array of allowed actions by this user on this node. Used to build form buttons and link. Array is created by Common::allowedActions
GridActionForm::$common protected property
GridActionForm::$error protected property Set to true when no items are selected on the form
GridActionForm::$fids protected property String containing Id's of the selected files
GridActionForm::$helper protected property
GridActionForm::$nid protected property
GridActionForm::$node protected property Node holding this form
GridActionForm::$relativeFid protected property fid of the relative root
GridActionForm::$relativeRoot protected property The relative root for this file listing. The complete location of a file is determined by folder_path + relativeRoot = path: local//: path_to_folderpath/folder1/folder2/file.txt In this example "folder1/folder2" is the relativeRoot
GridActionForm::ajaxValidate public function
GridActionForm::buildForm public function Overrides FormInterface::buildForm
GridActionForm::createButton public function
GridActionForm::createLink protected function
GridActionForm::createNormalButton public function
GridActionForm::getFids protected function
GridActionForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
GridActionForm::selectAll public function
GridActionForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
GridActionForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
LinkGeneratorTrait::$linkGenerator protected property The link generator. 1
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.