public function InsertViewDialog::renderArgument in Advanced Insert View 8
Same name and namespace in other branches
- 2.0.x src/Form/InsertViewDialog.php \Drupal\insert_view_adv\Form\InsertViewDialog::renderArgument()
Create the argument field
_state
Parameters
$form:
$view_block:
$num:
Throws
\Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
\Drupal\Component\Plugin\Exception\PluginNotFoundException
1 call to InsertViewDialog::renderArgument()
- InsertViewDialog::buildForm in src/
Form/ InsertViewDialog.php - Form constructor.
File
- src/
Form/ InsertViewDialog.php, line 127
Class
- InsertViewDialog
- Class InsertViewDialog
Namespace
Drupal\insert_view_adv\FormCode
public function renderArgument(&$form, $form_state, $view_block, $num) {
if (!empty($form['#view_arguments'][$view_block][$num])) {
$argument = $form['#view_arguments'][$view_block][$num];
// get field info
$info = InsertViewDialog::getFieldInfo($argument);
$field_info = $info['info'];
$bundle_info = $info['bundle_info'];
if ($field_info) {
$form['arguments']['argument'][$num] = [
'#type' => $field_info
->getType() == 'entity_reference' ? 'entity_autocomplete' : 'textfield',
'#title' => empty($bundle_info) ? $field_info
->getLabel() : $bundle_info
->getLabel(),
'#description' => empty($bundle_info) ? $field_info
->getDescription() : $bundle_info
->getDescription(),
'#default_value' => isset($this
->getUserInput($form_state, 'arguments')[$num]) ? $this
->getUserInput($form_state, 'arguments')[$num] : NULL,
];
// If it is entity reference and some more settings.
if ($field_info
->getType() == 'entity_reference') {
$info_settings = $field_info
->getSettings();
$bundle_settings = $bundle_info
->getSettings();
$form['arguments']['argument'][$num]['#target_type'] = $info_settings['target_type'];
$form['arguments']['argument'][$num]['#selection_handler'] = $bundle_settings['handler'];
if (!empty($bundle_settings['handler_settings'])) {
$form['arguments']['argument'][$num]['#selection_settings'] = $bundle_settings['handler_settings'];
}
if (isset($form['arguments']['argument'][$num]['#default_value'])) {
// Default value could be only entity, let's load one
$entity_storage = $this->entityTypeManager
->getStorage($info_settings['target_type']);
$entity = $entity_storage
->load($form['arguments']['argument'][$num]['#default_value']);
$form['arguments']['argument'][$num]['#default_value'] = $entity;
}
else {
$form['arguments']['argument'][$num]['#default_value'] = NULL;
}
}
}
else {
$argument_definition = reset($argument);
$property = $argument_definition['id'];
$entity_definitions = $this->entityTypeManager
->getDefinitions();
$not_found = TRUE;
if (!empty($argument_definition['relationship']) && $argument_definition['relationship'] == 'none') {
foreach ($entity_definitions as $entity_type_id => $definition) {
if (!$definition
->getBaseTable()) {
continue;
}
$tables = [
$definition
->getBaseTable(),
$definition
->getDataTable(),
];
if (in_array($argument_definition['table'], $tables)) {
try {
$base_fields = $this->entityFieldManager
->getBaseFieldDefinitions($entity_type_id);
if ($definition
->getKey('id') == $property || !empty($base_fields[$property]) && $base_fields[$property]
->getType() == 'entity_reference') {
$field_info = $base_fields[$property];
$target_entity_type_id = $field_info
->getFieldStorageDefinition()
->getSetting('target_type');
$entity_storage = $this->entityTypeManager
->getStorage($target_entity_type_id);
$entity = $entity_storage
->load($this
->getUserInput($form_state, 'arguments')[$num]);
$form['arguments']['argument'][$num] = [
'#type' => 'entity_autocomplete',
'#title' => $field_info
->getLabel(),
'#description' => $field_info
->getDescription(),
'#target_type' => $target_entity_type_id,
'#default_value' => $entity,
];
$not_found = FALSE;
}
} catch (\LogicException $e) {
continue;
}
}
}
}
if ($not_found) {
$default = $this
->getUserInput($form_state, 'arguments');
$form['arguments']['argument'][$num] = [
'#type' => 'textfield',
'#title' => $property,
'#default_value' => isset($default[$num]) ? $default[$num] : NULL,
];
}
}
}
}