View source
<?php
namespace Drupal\filebrowser\Services;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Link;
use Drupal\node\NodeInterface;
class FormHelper extends ControllerBase {
protected $node;
public function initForm(&$form, NodeInterface $node) {
$this->node = $node;
$form['nid'] = [
'#type' => 'value',
'#value' => $this->node
->id(),
];
$form['fids'] = [
'#type' => 'value',
'#value' => null,
];
$form['#attached']['library'][] = 'core/drupal.dialog.ajax';
$form['#attached']['library'][] = 'filebrowser/filebrowser-styles';
$form['#attached']['library'][] = 'core/jquery.form';
}
public function createActionBar(&$form, array $actions, $relative_fid) {
$form['action'] = [
'#type' => 'container',
'#attributes' => [
'id' => [
'form-action-actions-wrapper',
],
],
];
foreach ($actions as $action) {
switch ($action['type']) {
case 'link':
$form['action'][$action['operation']] = $this
->createLink($action, $this->node, $relative_fid);
break;
case 'button':
$form['action'][$action['operation']] = $this
->createAjaxButton($action);
break;
default:
$form['action'][$action['operation']] = $this
->createNormalButton($action);
}
}
}
public function createLink(array $action, NodeInterface $node, $relative_fid) {
$route = 'filebrowser.action';
$is_add_folder_link = $action['operation'] == 'folder' ? 'add-folder-link' : '';
$link_options = [
'attributes' => [
'class' => [
'use-ajax',
'button',
'btn',
'btn-default',
$is_add_folder_link,
],
],
];
$route_options = [
'op' => $action['operation'],
'method' => 'nojs',
'nid' => $node
->id(),
'query_fid' => $relative_fid,
];
return [
'#markup' => Link::createFromRoute($action['title'], $route, $route_options, $link_options)
->toString(),
];
}
public function createAjaxButton($action) {
return [
'#type' => 'submit',
'#return_value' => $action['operation'],
'#value' => $action['title'],
'#name' => $action['operation'],
'#title' => $action['title'],
'#attributes' => [
'class' => [
'use-ajax-submit',
],
],
'#validate' => [
'::ajaxValidate',
],
];
}
public function createNormalButton($action) {
return [
'#type' => 'submit',
'#return_value' => $action['operation'],
'#value' => $action['title'],
'#name' => $action['operation'],
'#title' => $action['title'],
];
}
}