class ActionForm in Filebrowser 8.2
Same name and namespace in other branches
- 3.x src/Form/ActionForm.php \Drupal\filebrowser\Form\ActionForm
 
Class ActionForm.
@package Drupal\filebrowser\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, LinkGeneratorTrait, RedirectDestinationTrait, UrlGeneratorTrait, StringTranslationTrait
- class \Drupal\filebrowser\Form\ActionForm
 
 
Expanded class hierarchy of ActionForm
File
- src/
Form/ ActionForm.php, line 15  
Namespace
Drupal\filebrowser\FormView 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
| 
            Name | 
                  Modifiers | Type | Description | Overrides | 
|---|---|---|---|---|
| 
            ActionForm:: | 
                  protected | property | ||
| 
            ActionForm:: | 
                  protected | property | Set to true when no items are selected on the form | |
| 
            ActionForm:: | 
                  protected | property | String containing Id's of the selected files | |
| 
            ActionForm:: | 
                  protected | property | ||
| 
            ActionForm:: | 
                  protected | property | ||
| 
            ActionForm:: | 
                  protected | property | fid of the relative root | |
| 
            ActionForm:: | 
                  public | function | ||
| 
            ActionForm:: | 
                  public | function | 
            Overrides FormInterface:: | 
                  |
| 
            ActionForm:: | 
                  protected | function | ||
| 
            ActionForm:: | 
                  public | function | 
            Returns a unique string identifying the form. Overrides FormInterface:: | 
                  |
| 
            ActionForm:: | 
                  public | function | 
            Form submission handler. Overrides FormInterface:: | 
                  |
| 
            ActionForm:: | 
                  public | function | 
            Form validation handler. Overrides FormBase:: | 
                  |
| 
            DependencySerializationTrait:: | 
                  protected | property | An array of entity type IDs keyed by the property name of their storages. | |
| 
            DependencySerializationTrait:: | 
                  protected | property | An array of service IDs keyed by property name used for serialization. | |
| 
            DependencySerializationTrait:: | 
                  public | function | 1 | |
| 
            DependencySerializationTrait:: | 
                  public | function | 2 | |
| 
            FormBase:: | 
                  protected | property | The config factory. | 1 | 
| 
            FormBase:: | 
                  protected | property | The request stack. | 1 | 
| 
            FormBase:: | 
                  protected | property | The route match. | |
| 
            FormBase:: | 
                  protected | function | Retrieves a configuration object. | |
| 
            FormBase:: | 
                  protected | function | Gets the config factory for this form. | 1 | 
| 
            FormBase:: | 
                  private | function | Returns the service container. | |
| 
            FormBase:: | 
                  public static | function | 
            Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: | 
                  87 | 
| 
            FormBase:: | 
                  protected | function | Gets the current user. | |
| 
            FormBase:: | 
                  protected | function | Gets the request object. | |
| 
            FormBase:: | 
                  protected | function | Gets the route match. | |
| 
            FormBase:: | 
                  protected | function | Gets the logger for a specific channel. | |
| 
            FormBase:: | 
                  protected | function | 
            Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait:: | 
                  |
| 
            FormBase:: | 
                  public | function | Resets the configuration factory. | |
| 
            FormBase:: | 
                  public | function | Sets the config factory for this form. | |
| 
            FormBase:: | 
                  public | function | Sets the request stack object to use. | |
| 
            LinkGeneratorTrait:: | 
                  protected | property | The link generator. | 1 | 
| 
            LinkGeneratorTrait:: | 
                  protected | function | Returns the link generator. | |
| 
            LinkGeneratorTrait:: | 
                  protected | function | Renders a link to a route given a route name and its parameters. | |
| 
            LinkGeneratorTrait:: | 
                  public | function | Sets the link generator service. | |
| 
            LoggerChannelTrait:: | 
                  protected | property | The logger channel factory service. | |
| 
            LoggerChannelTrait:: | 
                  protected | function | Gets the logger for a specific channel. | |
| 
            LoggerChannelTrait:: | 
                  public | function | Injects the logger channel factory. | |
| 
            MessengerTrait:: | 
                  protected | property | The messenger. | 29 | 
| 
            MessengerTrait:: | 
                  public | function | Gets the messenger. | 29 | 
| 
            MessengerTrait:: | 
                  public | function | Sets the messenger. | |
| 
            RedirectDestinationTrait:: | 
                  protected | property | The redirect destination service. | 1 | 
| 
            RedirectDestinationTrait:: | 
                  protected | function | Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url. | |
| 
            RedirectDestinationTrait:: | 
                  protected | function | Returns the redirect destination service. | |
| 
            RedirectDestinationTrait:: | 
                  public | function | Sets the redirect destination service. | |
| 
            StringTranslationTrait:: | 
                  protected | property | The string translation service. | 1 | 
| 
            StringTranslationTrait:: | 
                  protected | function | Formats a string containing a count of items. | |
| 
            StringTranslationTrait:: | 
                  protected | function | Returns the number of plurals supported by a given language. | |
| 
            StringTranslationTrait:: | 
                  protected | function | Gets the string translation service. | |
| 
            StringTranslationTrait:: | 
                  public | function | Sets the string translation service to use. | 2 | 
| 
            StringTranslationTrait:: | 
                  protected | function | Translates a string to the current language or to a given language. | |
| 
            UrlGeneratorTrait:: | 
                  protected | property | The url generator. | |
| 
            UrlGeneratorTrait:: | 
                  protected | function | Returns the URL generator service. | |
| 
            UrlGeneratorTrait:: | 
                  public | function | Sets the URL generator service. | |
| 
            UrlGeneratorTrait:: | 
                  protected | function | Generates a URL or path for a specific route based on the given parameters. |