public function BusinessRulesViewsSelection::buildConfigurationForm in Business Rules 8
Same name and namespace in other branches
- 2.x src/Plugin/EntityReferenceSelection/BusinessRulesViewsSelection.php \Drupal\business_rules\Plugin\EntityReferenceSelection\BusinessRulesViewsSelection::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 \Drupal\Core\Render\Element\FormElement.
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 form. Calling code should pass on a subform state created through \Drupal\Core\Form\SubformState::createForSubform().
Return value
array The form structure.
Overrides PluginFormInterface::buildConfigurationForm
File
- src/
Plugin/ EntityReferenceSelection/ BusinessRulesViewsSelection.php, line 254
Class
- BusinessRulesViewsSelection
- Plugin override of the 'selection' entity_reference.
Namespace
Drupal\business_rules\Plugin\EntityReferenceSelectionCode
public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
$handler_settings = $this
->getHandlerSettings();
$view_settings = !empty($handler_settings['business_rules_view']) ? $handler_settings['business_rules_view'] : [];
$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 = [];
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['business_rules_view']['#element_validate'] = [
[
get_called_class(),
'settingsFormValidate',
],
];
if ($options) {
$form['business_rules_view']['help']['#markup'] = t('This plugin do not works for autocomplete form widget. Make sure you have selected "Select list" or "Check boxes/radio buttons" at "Manage form display" tab.');
$default = !empty($view_settings['view_name']) ? $view_settings['view_name'] . ':' . $view_settings['display_name'] : NULL;
$form['business_rules_view']['view_and_display'] = [
'#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>',
];
/** @var \Drupal\field\Entity\FieldConfig $field_config */
$field_config = $this->util->request
->get('field_config');
$entity_type = $field_config
->getTargetEntityTypeId();
$bundle = $field_config
->getTargetBundle();
$fields = $this->util
->getBundleEditableFields($entity_type, $bundle);
$fields_options = [];
if (count($fields)) {
foreach ($fields as $key => $name) {
// Do not include the dependent field itself.
if ($key !== 'title' && $key !== $field_config
->getName()) {
$fields_options[$key] = $name;
}
}
}
$default = !empty($view_settings['parent_field']) ? $view_settings['parent_field'] : NULL;
$form['business_rules_view']['parent_field'] = [
'#type' => 'select',
'#title' => t('Parent field'),
'#options' => $fields_options,
'#required' => TRUE,
'#description' => t('The field which this field depends. When the parent field value is changed, the available options for this field will be updated using the parent field value as the first argument followed by any particular other argument imputed in the "Views arguments".'),
'#default_value' => $default,
];
$default = !empty($view_settings['reference_parent_by_uuid']) ? $view_settings['reference_parent_by_uuid'] : FALSE;
$form['business_rules_view']['reference_parent_by_uuid'] = [
'#type' => 'checkbox',
'#title' => $this
->t('Reference parent by UUID instead of entity ID?'),
'#default_value' => $default,
'#description' => $this
->t('Required if the parent argument in your view (selected above) accepts UUIDs instead of entity IDs (to ensure configuration portability between sites).'),
];
$default = !empty($view_settings['arguments']) ? implode(', ', $view_settings['arguments']) : '';
$form['business_rules_view']['arguments'] = [
'#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['business_rules_view']['no_view_help'] = [
'#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>.', [
':create' => Url::fromRoute('views_ui.add')
->toString(),
':existing' => Url::fromRoute('entity.view.collection')
->toString(),
]) . '</p>',
];
}
else {
$form['business_rules_view']['no_view_help']['#markup'] = '<p>' . $this
->t('No eligible views were found.') . '</p>';
}
}
return $form;
}