Newer
Older
Angie Byron
committed
<?php
/**
* @file
Angie Byron
committed
* Contains \Drupal\views_ui\Form\Ajax\ConfigHandlerGroup.
Angie Byron
committed
*/
namespace Drupal\views_ui\Form\Ajax;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
Dries Buytaert
committed
use Drupal\views\Views;
use Drupal\views\ViewEntityInterface;
Angie Byron
committed
use Drupal\views\ViewExecutable;
/**
Angie Byron
committed
* Provides a form for configuring grouping information for a Views UI handler.
Angie Byron
committed
*/
Angie Byron
committed
class ConfigHandlerGroup extends ViewsFormBase {
Angie Byron
committed
/**
Angie Byron
committed
* Constucts a new ConfigHandlerGroup object.
Angie Byron
committed
*/
public function __construct($type = NULL, $id = NULL) {
$this->setType($type);
$this->setID($id);
}
/**
* {@inheritdoc}
Angie Byron
committed
*/
public function getFormKey() {
Angie Byron
committed
return 'handler-group';
Angie Byron
committed
}
/**
* {@inheritdoc}
Angie Byron
committed
*/
public function getForm(ViewEntityInterface $view, $display_id, $js, $type = NULL, $id = NULL) {
Angie Byron
committed
$this->setType($type);
$this->setID($id);
return parent::getForm($view, $display_id, $js);
}
/**
* {@inheritdoc}
Angie Byron
committed
*/
Alex Pott
committed
public function getFormId() {
Angie Byron
committed
return 'views_ui_config_item_group_form';
}
/**
* {@inheritdoc}
Angie Byron
committed
*/
Dries Buytaert
committed
public function buildForm(array $form, FormStateInterface $form_state) {
Angie Byron
committed
$view = $form_state->get('view');
$display_id = $form_state->get('display_id');
$type = $form_state->get('type');
$id = $form_state->get('id');
Angie Byron
committed
$form = array(
'options' => array(
'#tree' => TRUE,
'#theme_wrappers' => array('container'),
'#attributes' => array('class' => array('scroll'), 'data-drupal-views-scroll' => TRUE),
Angie Byron
committed
),
);
Alex Pott
committed
$executable = $view->getExecutable();
Angie Byron
committed
if (!$executable->setDisplay($display_id)) {
Alex Pott
committed
$form['markup'] = array('#markup' => $this->t('Invalid display id @display', array('@display' => $display_id)));
return $form;
Angie Byron
committed
}
$executable->initQuery();
Angie Byron
committed
$item = $executable->getHandler($display_id, $type, $id);
Angie Byron
committed
if ($item) {
$handler = $executable->display_handler->getHandler($type, $id);
if (empty($handler)) {
$form['markup'] = array('#markup' => $this->t("Error: handler for @table > @field doesn't exist!", array('@table' => $item['table'], '@field' => $item['field'])));
Angie Byron
committed
}
else {
$handler->init($executable, $executable->display_handler, $item);
catch
committed
$types = ViewExecutable::getHandlerTypes();
Angie Byron
committed
$form['#title'] = $this->t('Configure aggregation settings for @type %item', array('@type' => $types[$type]['lstitle'], '%item' => $handler->adminLabel()));
Angie Byron
committed
$handler->buildGroupByForm($form['options'], $form_state);
Angie Byron
committed
$form_state->set('handler', $handler);
Angie Byron
committed
}
$view->getStandardButtons($form, $form_state, 'views_ui_config_item_group_form');
}
return $form;
}
/**
* {@inheritdoc}
Angie Byron
committed
*/
Dries Buytaert
committed
public function submitForm(array &$form, FormStateInterface $form_state) {
Angie Byron
committed
$view = $form_state->get('view');
$item = &$form_state->get('handler')->options;
$type = $form_state->get('type');
Angie Byron
committed
Dries Buytaert
committed
$handler = Views::handlerManager($type)->getHandler($item);
Angie Byron
committed
$executable = $view->getExecutable();
Angie Byron
committed
$handler->init($executable, $executable->display_handler, $item);
$handler->submitGroupByForm($form, $form_state);
// Store the item back on the view
Angie Byron
committed
$executable->setHandler($form_state->get('display_id'), $form_state->get('type'), $form_state->get('id'), $item);
Angie Byron
committed
// Write to cache
Angie Byron
committed
$view->cacheSet();