class GridActionForm in Filebrowser 3.x
Same name and namespace in other branches
- 8.2 src/Form/GridActionForm.php \Drupal\filebrowser\Form\GridActionForm
Class GridActionForm.
@package Drupal\filebrowser\Form
Hierarchy
- class \Drupal\Core\Form\FormBase implements ContainerInjectionInterface, FormInterface uses DependencySerializationTrait, LoggerChannelTrait, MessengerTrait, RedirectDestinationTrait, StringTranslationTrait
- class \Drupal\filebrowser\Form\GridActionForm
Expanded class hierarchy of GridActionForm
File
- src/
Form/ GridActionForm.php, line 19
Namespace
Drupal\filebrowser\FormView 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
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
protected | property | ||
DependencySerializationTrait:: |
public | function | 2 | |
DependencySerializationTrait:: |
public | function | 2 | |
FormBase:: |
protected | property | The config factory. | 3 |
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. | 3 |
FormBase:: |
private | function | Returns the service container. | |
FormBase:: |
public static | function |
Instantiates a new instance of this class. Overrides ContainerInjectionInterface:: |
105 |
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. | |
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. | |
GridActionForm:: |
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:: |
protected | property | ||
GridActionForm:: |
protected | property | Set to true when no items are selected on the form | |
GridActionForm:: |
protected | property | String containing Id's of the selected files | |
GridActionForm:: |
protected | property | ||
GridActionForm:: |
protected | property | ||
GridActionForm:: |
protected | property | Node holding this form | |
GridActionForm:: |
protected | property | fid of the relative root | |
GridActionForm:: |
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:: |
public | function | ||
GridActionForm:: |
public | function |
Overrides FormInterface:: |
|
GridActionForm:: |
public | function | ||
GridActionForm:: |
protected | function | ||
GridActionForm:: |
public | function | ||
GridActionForm:: |
protected | function | ||
GridActionForm:: |
public | function |
Returns a unique string identifying the form. Overrides FormInterface:: |
|
GridActionForm:: |
public | function | ||
GridActionForm:: |
public | function |
Form submission handler. Overrides FormInterface:: |
|
GridActionForm:: |
public | function |
Form validation handler. Overrides FormBase:: |
|
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. | 27 |
MessengerTrait:: |
public | function | Gets the messenger. | 27 |
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. | 4 |
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. |