You are here

class ActionForm in Filebrowser 8.2

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

Class ActionForm.

@package Drupal\filebrowser\Form

Hierarchy

Expanded class hierarchy of ActionForm

File

src/Form/ActionForm.php, line 15

Namespace

Drupal\filebrowser\Form
View source
class ActionForm 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 int
   * fid of the relative root
   */
  protected $relativeFid;

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

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

  /**
   * {@inheritdoc}
   */

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

  /**
   * @var \Drupal\filebrowser\Services\FormHelper
   */
  protected $helper;

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

  /**
   * @param array $params
   * Array with filebrowser data keyed:
   * 'header': the header to build the table select
   * 'rows' : array of file info to display
   * 'actions': Form actions that are allowed for current user
   *  'dbFileList': all the ifo about the files:
   * {@inheritdoc}
   */
  public function buildForm(array $form, FormStateInterface $form_state, $params = null) {

    /** @var NodeInterface $node */
    $this->error = false;
    $node = $params['node'];
    $actions = $params['actions'];
    $this->nid = $node
      ->id();
    $this->relativeFid = empty($params['dbFileList']['data']['fid']) ? 0 : $params['dbFileList']['data']['fid'];
    $this->common = \Drupal::service('filebrowser.common');
    $this->helper = \Drupal::service('form.helper');

    // Initiate the form according default filebrowser requirements
    $form = [];
    $this->helper
      ->initForm($form, $node);

    // Create the form action button and links (Upload, Rename etc.)
    $this->helper
      ->createActionBar($form, $actions, $this->relativeFid);

    // prepare the $rows array for $options
    $options = [];
    foreach ($params['rows'] as $row) {
      $fid = $row['fid'];
      unset($row['fid']);
      foreach ($row as $key => $value) {
        $options[$fid][$key] = [
          'data' => $value,
        ];
      }
    }
    $form['table'] = [
      '#type' => 'tableselect',
      '#tableselect' => true,
      '#header' => $params['header'],
      '#options' => $params['rows'],
      '#multiple' => TRUE,
      '#empty' => $this
        ->t('This directory is empty.'),
    ];

    // search $options array to find the "Up Dir" line and de-activate the checkbox
    foreach ($params['rows'] as $key => $option) {
      if (empty($option['fid'])) {
        $form['table'][$key]['#disabled'] = true;
      }
    }
    return $form;
  }

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

    // \Drupal::logger('filebrowser')->notice('NORMAL ACTION FORM VALIDATE');
    // 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) {

    // \Drupal::logger('filebrowser')->notice('AJAX FORM VALIDATE');
    // 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)) {

      //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.
    // \Drupal::logger('filebrowser')->notice('SUBMIT FORM');
    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) {
    foreach ($form_state
      ->getValue('table') as $key => $value) {
      if ($value) {
        $fids[] = $form['table']['#options'][$key]['fid'];
      }
    }
    $selected_fids = empty($fids) ? null : implode(',', $fids);
    return $selected_fids;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ActionForm::$common protected property
ActionForm::$error protected property Set to true when no items are selected on the form
ActionForm::$fids protected property String containing Id's of the selected files
ActionForm::$helper protected property
ActionForm::$nid protected property
ActionForm::$relativeFid protected property fid of the relative root
ActionForm::ajaxValidate public function
ActionForm::buildForm public function Overrides FormInterface::buildForm
ActionForm::getFids protected function
ActionForm::getFormId public function Returns a unique string identifying the form. Overrides FormInterface::getFormId
ActionForm::submitForm public function Form submission handler. Overrides FormInterface::submitForm
ActionForm::validateForm public function Form validation handler. Overrides FormBase::validateForm
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.
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.