public function ViewsSelection::buildConfigurationForm in Zircon Profile 8
Same name and namespace in other branches
- 8.0 core/modules/views/src/Plugin/EntityReferenceSelection/ViewsSelection.php \Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection::buildConfigurationForm()
Form constructor.
Plugin forms are embedded in other forms. In order to know where the plugin form is located in the parent form, #parents and #array_parents must be known, but these are not available during the initial build phase. In order to have these properties available when building the plugin form's elements, let this method return a form element that has a #process callback and build the rest of the form in the callback. By the time the callback is executed, the element's #parents and #array_parents properties will have been set by the form API. For more documentation on #parents and #array_parents, see https://api.drupal.org/api/drupal/developer!topics!forms_api_reference.h....
Parameters
array $form: An associative array containing the initial structure of the plugin form.
\Drupal\Core\Form\FormStateInterface $form_state: The current state of the complete form.
Return value
array The form structure.
Overrides PluginFormInterface::buildConfigurationForm
File
- core/
modules/ views/ src/ Plugin/ EntityReferenceSelection/ ViewsSelection.php, line 103 - Contains \Drupal\views\Plugin\EntityReferenceSelection\ViewsSelection.
Class
- ViewsSelection
- Plugin implementation of the 'selection' entity_reference.
Namespace
Drupal\views\Plugin\EntityReferenceSelectionCode
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$selection_handler_settings = $this->configuration['handler_settings'];
$view_settings = !empty($selection_handler_settings['view']) ? $selection_handler_settings['view'] : array();
$displays = Views::getApplicableViews('entity_reference_display');
// Filter views that list the entity type we want, and group the separate
// displays by view.
$entity_type = $this->entityManager
->getDefinition($this->configuration['target_type']);
$view_storage = $this->entityManager
->getStorage('view');
$options = array();
foreach ($displays as $data) {
list($view_id, $display_id) = $data;
$view = $view_storage
->load($view_id);
if (in_array($view
->get('base_table'), [
$entity_type
->getBaseTable(),
$entity_type
->getDataTable(),
])) {
$display = $view
->get('display');
$options[$view_id . ':' . $display_id] = $view_id . ' - ' . $display[$display_id]['display_title'];
}
}
// The value of the 'view_and_display' select below will need to be split
// into 'view_name' and 'view_display' in the final submitted values, so
// we massage the data at validate time on the wrapping element (not
// ideal).
$form['view']['#element_validate'] = array(
array(
get_called_class(),
'settingsFormValidate',
),
);
if ($options) {
$default = !empty($view_settings['view_name']) ? $view_settings['view_name'] . ':' . $view_settings['display_name'] : NULL;
$form['view']['view_and_display'] = array(
'#type' => 'select',
'#title' => $this
->t('View used to select the entities'),
'#required' => TRUE,
'#options' => $options,
'#default_value' => $default,
'#description' => '<p>' . $this
->t('Choose the view and display that select the entities that can be referenced.<br />Only views with a display of type "Entity Reference" are eligible.') . '</p>',
);
$default = !empty($view_settings['arguments']) ? implode(', ', $view_settings['arguments']) : '';
$form['view']['arguments'] = array(
'#type' => 'textfield',
'#title' => $this
->t('View arguments'),
'#default_value' => $default,
'#required' => FALSE,
'#description' => $this
->t('Provide a comma separated list of arguments to pass to the view.'),
);
}
else {
if ($this->currentUser
->hasPermission('administer views') && $this->moduleHandler
->moduleExists('views_ui')) {
$form['view']['no_view_help'] = array(
'#markup' => '<p>' . $this
->t('No eligible views were found. <a href=":create">Create a view</a> with an <em>Entity Reference</em> display, or add such a display to an <a href=":existing">existing view</a>.', array(
':create' => Url::fromRoute('views_ui.add')
->toString(),
':existing' => Url::fromRoute('entity.view.collection')
->toString(),
)) . '</p>',
);
}
else {
$form['view']['no_view_help']['#markup'] = '<p>' . $this
->t('No eligible views were found.') . '</p>';
}
}
return $form;
}