Please help with the following:
I need a new widget (just a slightly altered select) that can be setup from the UI by anyone. I have done the following:
Created a new custom module and enabled it.
Created a new widget.
<?php
namespace Drupal\filtered_select\Plugin\Field\FieldWidget;
use Drupal\Core\Field\FieldItemListInterface;
use Drupal\Core\Field\Plugin\Field\FieldWidget\OptionsSelectWidget;
use Drupal\Core\Form\FormStateInterface;
class FilteredSelectWidget extends OptionsSelectWidget {
public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {
$element = parent::formElement($items, $delta, $element, $form, $form_state);
$element['#type'] = 'fselect';
return $element;
}
}
- Created new element so I can switch the theme. (not sure if this is necessary)
<?php
namespace Drupal\filtered_select\Element;
use Drupal\Core\Render\Element\Select;
class FilteredSelect extends Select {
public function getInfo() {
$info = parent::getInfo();
$info['#theme'] = 'fselect';
return $info;
}
}
- It did not work otherwise so I also got a hook_theme up in my module file like this:
function filtered_select_theme($existing, $type, $theme, $path): array {
return [
'fselect' => [
'render element' => 'element',
'template' => 'fselect',
],
];
}
- I just copied the template from core into my custom module and it works in the sense that it prints the right template, I have not yet altered this one, I just want to get the same old core functionality working under another name first.
Issue:
My new render element seems to be missing the options array and I can't figure out why. I've done really basic overwrites and I cannot understand why it does not build the options array.
Twig Var Dumps:
--- THIS IS THE CORE SELECT ---
array:15 [▼
"element" =>array:41 [▶]
"theme_hook_original" => "select"
"attributes" =>Drupal\Core\Template\Attribute {
"title_attributes" =>Drupal\Core\Template\Attribute {
"content_attributes" =>Drupal\Core\Template\Attribute {
"title_prefix" => []
"title_suffix" => []
"db_is_active" =>true
"is_admin" =>true
"logged_in" =>true
"user" =>Drupal\Core\Session\AccountProxy {
"directory" => "core/themes/seven"
"options" =>array:3 [▼0 =>array:4 [▶]
1 =>array:3 [▶]
2 =>array:3 [▶]
]
"#cache" =>array:1 [▶]
"theme_hook_suggestions" => []
]
--- THIS IS MY 'NEW' Filtered Select ---
array:14 [▼
"element" =>array:41 [▶]
"theme_hook_original" => "fselect"
"attributes" =>Drupal\Core\Template\Attribute {
"title_attributes" =>Drupal\Core\Template\Attribute {
"content_attributes" =>Drupal\Core\Template\Attribute {
"title_prefix" => []
"title_suffix" => []
"db_is_active" =>true
"is_admin" =>true
"logged_in" =>true
"user" =>Drupal\Core\Session\AccountProxy {
"directory" => "core/themes/seven"
"#cache" =>array:1 [▶]
"theme_hook_suggestions" => []
]
As one can see, I'm missing the 'options' array and I cannot understand why. Please advise.