Newer
Older
Angie Byron
committed
<?php
/**
* @file
Angie Byron
committed
* Contains \Drupal\views_ui\Form\Ajax\ConfigHandler.
Angie Byron
committed
*/
namespace Drupal\views_ui\Form\Ajax;
Dries Buytaert
committed
use Drupal\Core\Form\FormStateInterface;
Angie Byron
committed
use Drupal\views\ViewStorageInterface;
use Drupal\views\ViewExecutable;
Dries Buytaert
committed
use Drupal\views\Views;
Angie Byron
committed
/**
* Provides a form for configuring an item in the Views UI.
*/
Angie Byron
committed
class ConfigHandler extends ViewsFormBase {
Angie Byron
committed
/**
Angie Byron
committed
* Constucts a new ConfigHandler 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';
Angie Byron
committed
}
/**
* {@inheritdoc}
Angie Byron
committed
*/
public function getForm(ViewStorageInterface $view, $display_id, $js, $type = NULL, $id = NULL) {
$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_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
$save_ui_cache = FALSE;
$executable->setDisplay($display_id);
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 {
catch
committed
$types = ViewExecutable::getHandlerTypes();
Angie Byron
committed
// If this item can come from the default display, show a dropdown
// that lets the user choose which display the changes should apply to.
if ($executable->display_handler->defaultableSections($types[$type]['plural'])) {
Angie Byron
committed
$section = $types[$type]['plural'];
$form_state->set('section', $section);
views_ui_standard_display_dropdown($form, $form_state, $section);
Angie Byron
committed
}
// A whole bunch of code to figure out what relationships are valid for
// this item.
$relationships = $executable->display_handler->getOption('relationships');
$relationship_options = array();
foreach ($relationships as $relationship) {
// relationships can't link back to self. But also, due to ordering,
// relationships can only link to prior relationships.
if ($type == 'relationship' && $id == $relationship['id']) {
break;
}
Dries Buytaert
committed
$relationship_handler = Views::handlerManager('relationship')->getHandler($relationship);
Angie Byron
committed
// ignore invalid/broken relationships.
if (empty($relationship_handler)) {
continue;
}
// If this relationship is valid for this type, add it to the list.
Dries Buytaert
committed
$data = Views::viewsData()->get($relationship['table']);
if (isset($data[$relationship['field']]['relationship']['base']) && $base = $data[$relationship['field']]['relationship']['base']) {
Angie Byron
committed
$base_fields = Views::viewsDataHelper()->fetchFields($base, $type, $executable->display_handler->useGroupBy());
if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
$relationship_handler->init($executable, $executable->display_handler, $relationship);
$relationship_options[$relationship['id']] = $relationship_handler->adminLabel();
}
Angie Byron
committed
}
}
if (!empty($relationship_options)) {
// Make sure the existing relationship is even valid. If not, force
// it to none.
Angie Byron
committed
$base_fields = Views::viewsDataHelper()->fetchFields($view->get('base_table'), $type, $executable->display_handler->useGroupBy());
Angie Byron
committed
if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
$relationship_options = array_merge(array('none' => $this->t('Do not use a relationship')), $relationship_options);
Angie Byron
committed
}
$rel = empty($item['relationship']) ? 'none' : $item['relationship'];
if (empty($relationship_options[$rel])) {
// Pick the first relationship.
$rel = key($relationship_options);
// We want this relationship option to get saved even if the user
// skips submitting the form.
Angie Byron
committed
$executable->setHandlerOption($display_id, $type, $id, 'relationship', $rel);
Angie Byron
committed
$save_ui_cache = TRUE;
}
$form['options']['relationship'] = array(
'#type' => 'select',
'#title' => $this->t('Relationship'),
Angie Byron
committed
'#options' => $relationship_options,
'#default_value' => $rel,
'#weight' => -500,
);
}
else {
$form['options']['relationship'] = array(
'#type' => 'value',
'#value' => 'none',
);
}
$form['#title'] = $this->t('Configure @type: @item', array('@type' => $types[$type]['lstitle'], '@item' => $handler->adminLabel()));
Angie Byron
committed
if (!empty($handler->definition['help'])) {
$form['options']['form_description'] = array(
'#markup' => $handler->definition['help'],
'#theme_wrappers' => array('container'),
'#attributes' => array('class' => array('form-item description')),
'#weight' => -1000,
);
}
$form['#section'] = $display_id . '-' . $type . '-' . $id;
// Get form from the handler.
$handler->buildOptionsForm($form['options'], $form_state);
Angie Byron
committed
$form_state->set('handler', $handler);
Angie Byron
committed
}
Angie Byron
committed
$name = $form_state->get('update_name');
Angie Byron
committed
$view->getStandardButtons($form, $form_state, 'views_ui_config_item_form', $name);
// Add a 'remove' button.
$form['actions']['remove'] = array(
Angie Byron
committed
'#type' => 'submit',
'#value' => $this->t('Remove'),
Angie Byron
committed
'#submit' => array(array($this, 'remove')),
'#limit_validation_errors' => array(array('override')),
'#ajax' => array(
'path' => current_path(),
),
Angie Byron
committed
);
}
if ($save_ui_cache) {
Alex Pott
committed
$view->cacheSet();
Angie Byron
committed
}
return $form;
}
/**
* {@inheritdoc}
Angie Byron
committed
*/
Dries Buytaert
committed
public function validateForm(array &$form, FormStateInterface $form_state) {
Angie Byron
committed
$form_state->get('handler')->validateOptionsForm($form['options'], $form_state);
Angie Byron
committed
if ($form_state->getErrors()) {
Angie Byron
committed
$form_state->set('rerender', TRUE);
Angie Byron
committed
}
}
/**
* {@inheritdoc}
Angie Byron
committed
*/
Dries Buytaert
committed
public function submitForm(array &$form, FormStateInterface $form_state) {
Angie Byron
committed
$view = $form_state->get('view');
$display_id = $form_state->get('display_id');
$id = $form_state->get('id');
$handler = $form_state->get('handler');
Angie Byron
committed
// Run it through the handler's submit function.
Angie Byron
committed
$handler->submitOptionsForm($form['options'], $form_state);
$item = $handler->options;
catch
committed
$types = ViewExecutable::getHandlerTypes();
Angie Byron
committed
// For footer/header $handler_type is area but $type is footer/header.
// For all other handle types it's the same.
Angie Byron
committed
$handler_type = $type = $form_state->get('type');
Angie Byron
committed
if (!empty($types[$type]['type'])) {
$handler_type = $types[$type]['type'];
}
$override = NULL;
Angie Byron
committed
$executable = $view->getExecutable();
Angie Byron
committed
if ($executable->display_handler->useGroupBy() && !empty($item['group_type'])) {
if (empty($executable->query)) {
$executable->initQuery();
}
$aggregate = $executable->query->getAggregationInfo();
Angie Byron
committed
if (!empty($aggregate[$item['group_type']]['handler'][$type])) {
$override = $aggregate[$item['group_type']]['handler'][$type];
}
}
// Create a new handler and unpack the options from the form onto it. We
// can use that for storage.
Dries Buytaert
committed
$handler = Views::handlerManager($handler_type)->getHandler($item, $override);
Angie Byron
committed
$handler->init($executable, $executable->display_handler, $item);
// Add the incoming options to existing options because items using
// the extra form may not have everything in the form here.
Angie Byron
committed
$options = $form_state->getValue('options') + $handler->options;
Angie Byron
committed
// This unpacks only options that are in the definition, ensuring random
// extra stuff on the form is not sent through.
$handler->unpackOptions($handler->options, $options, NULL, FALSE);
// Add any dependencies as the handler is saved. Put it here so
// it does not need to be declared in defineOptions().
if ($dependencies = $handler->getDependencies()) {
$handler->options['dependencies'] = $dependencies;
}
// Add the module providing the handler as a dependency as well.
$handler->options['dependencies']['module'][] = $handler->definition['provider'];
Angie Byron
committed
// Store the item back on the view
Angie Byron
committed
$executable->setHandler($display_id, $type, $id, $handler->options);
Angie Byron
committed
// Ensure any temporary options are removed.
Angie Byron
committed
if (isset($view->temporary_options[$type][$id])) {
unset($view->temporary_options[$type][$id]);
Angie Byron
committed
}
// Write to cache
Angie Byron
committed
$view->cacheSet();
Angie Byron
committed
}
/**
* Submit handler for removing an item from a view
*/
Dries Buytaert
committed
public function remove(&$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
// Store the item back on the view
Angie Byron
committed
list($was_defaulted, $is_defaulted) = $view->getOverrideValues($form, $form_state);
$executable = $view->getExecutable();
Angie Byron
committed
// If the display selection was changed toggle the override value.
if ($was_defaulted != $is_defaulted) {
Angie Byron
committed
$display = &$executable->displayHandlers->get($display_id);
Angie Byron
committed
$display->optionsOverride($form, $form_state);
}
Angie Byron
committed
$executable->removeHandler($display_id, $type, $id);
Angie Byron
committed
// Write to cache
Angie Byron
committed
$view->cacheSet();