Extend core select widget

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:

  1. Created a new custom module and enabled it.

  2. Created a new widget.

<?php  namespace Drupalfiltered_selectPluginFieldFieldWidget;  use DrupalCoreFieldFieldItemListInterface; use DrupalCoreFieldPluginFieldFieldWidgetOptionsSelectWidget; use DrupalCoreFormFormStateInterface;  /**  * Plugin implementation of the 'filtered_select' widget.  *  * @FieldWidget(  *   id = "filtered_select",  *   label = @Translation("Filtered Select"),  *   field_types = {  *      "entity_reference"  *   },  *   multiple_values = FALSE  * )  */ class FilteredSelectWidget extends OptionsSelectWidget {    /**    * {@inheritdoc}    */   public function formElement(FieldItemListInterface $items, $delta, array $element, array &$form, FormStateInterface $form_state) {     $element = parent::formElement($items, $delta, $element, $form, $form_state);     // Use the parent select functionality,     // just switch the element.     $element['#type'] = 'fselect';      return $element;   }  }  
  1. Created new element so I can switch the theme. (not sure if this is necessary)
<?php  namespace Drupalfiltered_selectElement;  use DrupalCoreRenderElementSelect;  /**  * Provides the filtered select element.  *   * @FormElement("fselect")  */ class FilteredSelect extends Select {    /**    * {@inheritdoc}    */   public function getInfo() {     $info = parent::getInfo();      // Theme switch.     $info['#theme'] = 'fselect';      return $info;   } }  
  1. It did not work otherwise so I also got a hook_theme up in my module file like this:
/**  * Implements hook_theme().  */ function filtered_select_theme($existing, $type, $theme, $path): array {     return [         'fselect' => [             'render element' => 'element',             'template' => 'fselect',         ],     ]; }    
  1. 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" =>DrupalCoreTemplateAttribute {#7543 ▶}   "title_attributes" =>DrupalCoreTemplateAttribute {#7932 ▶}   "content_attributes" =>DrupalCoreTemplateAttribute {#7896 ▶}   "title_prefix" => []   "title_suffix" => []   "db_is_active" =>true   "is_admin" =>true   "logged_in" =>true   "user" =>DrupalCoreSessionAccountProxy {#2354 ▶}   "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" =>DrupalCoreTemplateAttribute {#7543 ▶}   "title_attributes" =>DrupalCoreTemplateAttribute {#8034 ▶}   "content_attributes" =>DrupalCoreTemplateAttribute {#7708 ▶}   "title_prefix" => []   "title_suffix" => []   "db_is_active" =>true   "is_admin" =>true   "logged_in" =>true   "user" =>DrupalCoreSessionAccountProxy {#2354 ▶}   "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.

This article was republished from its original source.
Call Us: 1(800)730-2416

Pixeldust is a 20-year-old web development agency specializing in Drupal and WordPress and working with clients all over the country. With our best in class capabilities, we work with small businesses and fortune 500 companies alike. Give us a call at 1(800)730-2416 and let’s talk about your project.

FREE Drupal SEO Audit

Test your site below to see which issues need to be fixed. We will fix them and optimize your Drupal site 100% for Google and Bing. (Allow 30-60 seconds to gather data.)

Powered by

Extend core select widget

On-Site Drupal SEO Master Setup

We make sure your site is 100% optimized (and stays that way) for the best SEO results.

With Pixeldust On-site (or On-page) SEO we make changes to your site’s structure and performance to make it easier for search engines to see and understand your site’s content. Search engines use algorithms to rank sites by degrees of relevance. Our on-site optimization ensures your site is configured to provide information in a way that meets Google and Bing standards for optimal indexing.

This service includes:

  • Pathauto install and configuration for SEO-friendly URLs.
  • Meta Tags install and configuration with dynamic tokens for meta titles and descriptions for all content types.
  • Install and fix all issues on the SEO checklist module.
  • Install and configure XML sitemap module and submit sitemaps.
  • Install and configure Google Analytics Module.
  • Install and configure Yoast.
  • Install and configure the Advanced Aggregation module to improve performance by minifying and merging CSS and JS.
  • Install and configure Schema.org Metatag.
  • Configure robots.txt.
  • Google Search Console setup snd configuration.
  • Find & Fix H1 tags.
  • Find and fix duplicate/missing meta descriptions.
  • Find and fix duplicate title tags.
  • Improve title, meta tags, and site descriptions.
  • Optimize images for better search engine optimization. Automate where possible.
  • Find and fix the missing alt and title tag for all images. Automate where possible.
  • The project takes 1 week to complete.