Implements \Drupal\Core\Form\FormInterface::buildForm().
Overrides FormInterface::buildForm
public function buildForm(array $form, array &$form_state) {
$view =& $form_state['view'];
$display_id = $form_state['display_id'];
$type = $form_state['type'];
$id = $form_state['id'];
$form = array(
'options' => array(
'#tree' => TRUE,
'#theme_wrappers' => array(
'container',
),
'#attributes' => array(
'class' => array(
'scroll',
),
),
),
);
$executable = $view
->get('executable');
$save_ui_cache = FALSE;
if (!$executable
->setDisplay($display_id)) {
views_ajax_error(t('Invalid display id @display', array(
'@display' => $display_id,
)));
}
$item = $executable
->getItem($display_id, $type, $id);
if ($item) {
$handler = $executable->display_handler
->getHandler($type, $id);
if (empty($handler)) {
$form['markup'] = array(
'#markup' => t("Error: handler for @table > @field doesn't exist!", array(
'@table' => $item['table'],
'@field' => $item['field'],
)),
);
}
else {
$types = ViewExecutable::viewsHandlerTypes();
// 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'])) {
$form_state['section'] = $types[$type]['plural'];
views_ui_standard_display_dropdown($form, $form_state, $form_state['section']);
}
// 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;
}
$relationship_handler = views_get_handler($relationship, 'relationship');
// ignore invalid/broken relationships.
if (empty($relationship_handler)) {
continue;
}
// If this relationship is valid for this type, add it to the list.
$data = Views::viewsData()
->get($relationship['table']);
$base = $data[$relationship['field']]['relationship']['base'];
$base_fields = views_fetch_fields($base, $form_state['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();
}
}
if (!empty($relationship_options)) {
// Make sure the existing relationship is even valid. If not, force
// it to none.
$base_fields = views_fetch_fields($view
->get('base_table'), $form_state['type'], $executable->display_handler
->useGroupBy());
if (isset($base_fields[$item['table'] . '.' . $item['field']])) {
$relationship_options = array_merge(array(
'none' => t('Do not use a relationship'),
), $relationship_options);
}
$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.
$executable
->setItemOption($display_id, $type, $id, 'relationship', $rel);
$save_ui_cache = TRUE;
}
$form['options']['relationship'] = array(
'#type' => 'select',
'#title' => t('Relationship'),
'#options' => $relationship_options,
'#default_value' => $rel,
'#weight' => -500,
);
}
else {
$form['options']['relationship'] = array(
'#type' => 'value',
'#value' => 'none',
);
}
$form['#title'] = t('Configure @type: @item', array(
'@type' => $types[$type]['lstitle'],
'@item' => $handler
->adminLabel(),
));
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);
$form_state['handler'] =& $handler;
}
$name = NULL;
if (isset($form_state['update_name'])) {
$name = $form_state['update_name'];
}
$view
->getStandardButtons($form, $form_state, 'views_ui_config_item_form', $name);
// Add a 'remove' button.
$form['buttons']['remove'] = array(
'#type' => 'submit',
'#value' => t('Remove'),
'#submit' => array(
array(
$this,
'remove',
),
),
'#limit_validation_errors' => array(
array(
'override',
),
),
);
}
if ($save_ui_cache) {
$view
->cacheSet();
}
return $form;
}