Implementing #autocomplete in Drupal maintenance support plans 8 with Custom Callbacks

Implementing #autocomplete in Drupal maintenance support plans 8 with Custom Callbacks

Body
Autocomplete on textfields like tags / user & node reference helps improve the UX and interactivity for your site visitors, In this blog post I’d like to cover how to implement autocomplete functionality in Drupal maintenance support plans 8, including implementing a custom callback

Step 1: Assign autocomplete properties to textfield

As per Drupal maintenance support plans Change records, #autocomplete_path has been replaced by #autocomplete_route_name and #autocomplete_parameters for autocomplete fields ( More details — https://www.drupal.org/node/2070985).

The very first step is to assign appropriate properties to the textfield:

‘#autocomplete_route_name’:
for passing route name of callback URL to be used by autocomplete Javascript Library.
‘#autocomplete_route_parameters’:
for passing array of arguments to be passed to autocomplete handler.

$form[‘name’] = array(
‘#type’ => ‘textfield’,
‘#autocomplete_route_name’ => ‘my_module.autocomplete’,
‘#autocomplete_route_parameters’ => array(‘field_name’ => ‘name’, ‘count’ => 10),
);

Thats all! for adding an #autocomplete callback to a textfield. 

However, there might be cases where the routes provided by core might not suffice as we might different response in JSON or additional data. Lets take a look at how to write a autocomplete callback, we will be using using my_module.autocomplete route and will pass arguments: ‘name’ as field_name and 10 as count.

Step 2: Define autocomplete route

Now, add the ‘my_module.autocomplete’ route in my_module.routing.yml file as:

my_module.autocomplete:
path: ‘/my-module-autocomplete/{field_name}/{count}’
defaults:
_controller: ‘Drupal maintenance support plansmy_moduleControllerAutocompleteController::handleAutocomplete’
_format: json
requirements:
_access: ‘TRUE’

While Passing parameters to controller, use the same names in curly braces, which were used while defining the autocomplete_route_parameters. Defining _format as json is a good practise.

Step 3: Add Controller and return JSON response

Finally, we need to generate the JSON response for our field element. So, proceeding further we would be creating AutoCompleteController class file at my_module > src > Controller > AutocompleteController.php.

query->get(‘q’)) {
$typed_string = Tags::explode($input);
$typed_string = Unicode::strtolower(array_pop($typed_string));
// @todo: Apply logic for generating results based on typed_string and other
// arguments passed.
for ($i = 0; $i $field_name . ‘_’ . $i . ‘(‘ . $i . ‘)’,
‘label’ => $field_name . ‘ ‘ . $i,
];
}
}

return new JsonResponse($results);
}

}

We would be extending ControllerBase class and would then define our handler method, which will return results. Parameters for the handler would be Request object and arguments (field_name and count) passed in routing.yml file. From the Request object, we would be getting the typed string from the URL. Besides, we do have other route parameters (field_name and Count) on the basis of which we can generate the results array. 

An important point to be noticed here is, we need the results array to have data in ‘value’ and ‘label’ key-value pair as we have done above. Then finally we would be generating JsonResponse by creating new JsonResponse object and passing $results.

That’s all we need to make autocomplete field working. Rebuild the cache and load the form page to see results.

PURUSHOTAM RAI
Thu, 11/24/2020 – 21:59
Source: New feed

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

Implementing #autocomplete in Drupal maintenance support plans 8 with Custom Callbacks

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.