Welcome to Drupal Development Service third a part of our collection on writing Views question plugins. Partly 1, we talked about Drupal Development Service planning work that ought to precede coding. Partly 2, we coated Drupal Development Service fundamentals of truly writing a question plugin. On this remaining chapter, we are going to examine some enhancements to make your plugin extra polished and versatile. Exposing configuration choices Permit Website Admins to set their most well-liked models Drupal 10 Upkeep and Help Service metric or imperial. Most Fitbit endpoints settle for an choice to set Drupal Development Service models Drupal Development Service response is returned in. In case you are Canadian like me, you recognize that metric is preferable, however it’s additionally in our nature to be be good, so we must always expose a configuration choice to permit our American associates to indicate information of their anachronistic imperial models. (I jest, love you guys!) Exposing configuration choices for a question plugin is completed in two elements. First, construct Drupal Development Service UI and, second, make use of Drupal Development Service saved configuration. In our question plugin class, we’ll implement two strategies to assist us create Drupal Development Service UI, defineOptions and buildOptionsForm Drupal 10 Upkeep and Help Service /** * {@inheritdoc} */ protected operate defineOptions() { $choices = father or mother Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help ServicedefineOptions(); $choices[‘accept_lang’] = array( ‘default’ => NULL, ); return $choices; } /** * {@inheritdoc} */ public operate buildOptionsForm(&$type, FormStateInterface $form_state) { father or mother Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help ServicebuildOptionsForm($type, $form_state); $type[‘accept_lang’] = [ ‘#type’ => ‘select’, ‘#options’ => [ ” => $this->t(‘Metric’), ‘en_US’ => $this->t(‘US’), ‘en_GB’ => $this->t(‘UK’), ], ‘#title’ => $this->t(‘Unit system’), ‘#default_value’ => $this->choices[‘accept_lang’], ‘#description’ => $this->t(‘Set Drupal Development Service unit system to make use of for Fitbit API requests.’), ]; } With this finished, we must always see our configuration choices in Drupal Development Service Views UI underneath Superior > Question settings. undefined Nonetheless, it received’t work as a result of we’re not truly utilizing Drupal Development Service saved configuration but. To do this, we’ll add to our execute methodology in our question plugin Drupal 10 Upkeep and Help Service /** * {@inheritdoc} */ public operate execute(ViewExecutable $view) { // Set Drupal Development Service models based on Drupal Development Service setting on Drupal Development Service view. if (!empty($this->choices[‘accept_lang’])) { $this->fitbitClient->setAcceptLang($this->choices[‘accept_lang’]); } // Clip… } Question plugin choices can be found by way of $this->choices, which gives as a part of Drupal Development Service QueryPluginBase class that our Views plugin is extending. We use Drupal Development Service saved worth, along with a technique on Drupal Development Service Fitbit shopper service to set Drupal Development Service most well-liked models for all subsequent API requests Drupal 10 Upkeep and Help Service $this->fitbitClient->setAcceptLang($this->choices[‘accept_lang’]); . With that, web site admininstrators can set their most well-liked models, and Drupal Development Service outcome set will mirror that selection. Since that is Views and we’ve uncovered peak as a numeric discipline, Views core offers us a pleasant option to format Drupal Development Service information and suffix it with models so we find yourself with a cultured outcome. Simply edit Drupal Development Service discipline choices. undefined Subject plugin choices Including choices to customise Drupal Development Service look of Drupal Development Service avatar discipline. Views additionally permits us to have customized choices for our discipline plugins. In Drupal Development Service final article, we arrange a discipline plugin for avatar which makes use of Drupal Development Service avatar URI from Drupal Development Service API response and renders it as an <img> tag. Fitbit’s API truly gives two avatar measurement choices and it could be nice to go away it to Drupal Development Service web site administrator to determine which measurement to render. We’ll use discipline plugin choices to do this. As with question plugins, exposing configuration choices for a discipline plugin follows Drupal Development Service identical two elements, with one small addition. In our question plugin class, we’ll implement two strategies, defineOptions and buildOptionsForm , to construct Drupal Development Service UI Drupal 10 Upkeep and Help Service /** * {@inheritdoc} */ protected operate defineOptions() { $choices = father or mother Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help ServicedefineOptions(); $choices[‘avatar_size’] = [‘default’ => ‘avatar’]; return $choices; } /** * {@inheritdoc} */ public operate buildOptionsForm(&$type, FormStateInterface $form_state) { $type[‘avatar_size’] = [ ‘#type’ => ‘select’, ‘#title’ => $this->t(‘Image size’), ‘#options’ => [ ‘avatar’ => $this->t(‘Default (100px)’), ‘avatar150’ => $this->t(‘Medium (150px)’), ], ‘#default_value’ => $this->choices[‘avatar_size’], ‘#description’ => $this->t(‘Select Drupal Development Service measurement avatar you want to use.’), ]; father or mother Drupal 10 Upkeep and Help Service Drupal 10 Upkeep and Help ServicebuildOptionsForm($type, $form_state); } This ought to be pretty self explanatory; we’re defining a type ingredient for Drupal Development Service UI and, as soon as saved, Drupal Development Service configuration choice shall be saved in $this->choices[‘avatar_size’] . Drupal Development Service small addition I referred to earlier lies inside Drupal Development Service question plugin. Earlier than, we have been solely passing alongside Drupal Development Service single worth for avatar. Now that Drupal Development Service web site administrator has Drupal Development Service choice, we’ll wish to be certain each values for avatar are handed alongside in Drupal Development Service Views outcome. We try this, in Drupal Development Service question plugins execute methodology like so Drupal 10 Upkeep and Help Service $row[‘avatar’] = [ ‘avatar’ => $data[‘avatar’], ‘avatar150’ => $information[‘avatar150’], ]; As an alternative of a flat worth, we’re setting ‘avatar’ to an array with each values for avatar from Drupal Development Service API response. Then, again in Drupal Development Service discipline plugin, in Drupal Development Service render methodology, we take care to make use of Drupal Development Service acceptable measurement avatar based on Drupal Development Service choice chosen Drupal 10 Upkeep and Help Service /** * {@inheritdoc} */ public operate render(ResultRow $values) { $avatar = $this->getValue($values); if ($avatar) { return [ ‘#theme’ => ‘image’, ‘#uri’ => $avatar[$this->options[‘avatar_size’]], ‘#alt’ => $this->t(‘Avatar’), ]; } } We merely name $this->getValue($values), which is ready to pull out Drupal Development Service worth we wish from Drupal Development Service ResultRow object. Drupal Development Service render methodology receives a ResultRow object that has all of Drupal Development Service information for Drupal Development Service row. FieldPluginBase has a getValue methodology which we will entry since we’re extending FieldPluginBase . With that finished, we will now click on on Drupal Development Service avatar discipline in Drupal Development Service Views UI and set Drupal Development Service desired picture measurement Drupal 10 Upkeep and Help Service undefined Filter plugins Filtering Drupal Development Service leaderboard by consumer id. What if we wished to restrict Drupal Development Service outcome to solely a specific consumer? Say we wished to indicate a consumer’s Fitbit particulars on their consumer profile web page. For that, we’d have to filter Drupal Development Service outcome set by a consumer id. To make that occur, we’d like a Views filter plugin. Drupal Development Service first step is to outline Drupal Development Service discipline to filter on in hook_views_data() Drupal 10 Upkeep and Help Service /** * Implements hook_views_data(). */ operate fitbit_views_example_views_data() { // Base information and different discipline definitions… $information[‘fitbit_profile’][‘uid’] = [ ‘title’ => t(‘User id’), ‘help’ => t(‘ user id, not to be confused with Fitbit profile id.’), ‘field’ => [ ‘id’ => ‘standard’, ], ‘filter’ => [ ‘id’ => ‘fitbit_uid’, ], ]; return $information; } Drupal Development Service half we’re most involved with right here is Drupal Development Service ‘filter’ key. Its worth is an associative array with one key ‘id’, which we set to Drupal Development Service identify of Drupal Development Service filter plugin we’re going to create. Additionally, notice Drupal Development Service ‘discipline’ key, which makes Drupal Development Service consumer id accessible as a discipline in Drupal Development Service Views UI. It doesn’t damage so as to add it, and it additionally illustrates how plugins associated to a sure discipline (e.g. discipline, filter, and others like relationship and argument) are all outlined in Drupal Development Service identical array in hook_views_data(). So, for Drupal Development Service subsequent step, we’ll create this file Drupal 10 Upkeep and Help Service fitbit_views_example/src/Plugin/views/filter/Uid.php <?php namespace fitbit_views_examplePluginviewsfilter; /** * Easy filter to deal with filtering Fitbit outcomes by uid. * @ViewsFilter(“fitbit_uid”) */ class Uid extends FilterPluginBase { } To this point, that is typical 8 plugin scaffolding code. Drupal Development Service file is positioned in Drupal Development Service proper folder for Drupal Development Service plugin sort. Drupal Development Service namespace follows PSR-4 naming. Drupal Development Service annotation for Drupal Development Service plugin sort assigns an id to our plugin. Lastly, we lengthen Drupal Development Service base class supplied by Views for Drupal Development Service plugin sort. Now let’s take a look at Drupal Development Service specifics required for our filter plugin implementation Drupal 10 Upkeep and Help Service class Uid extends FilterPluginBase { public $no_operator = TRUE; /** * {@inheritdoc} */ protected operate valueForm(&$type, FormStateInterface $form_state) { $type[‘value’] = [ ‘#type’ => ‘textfield’, ‘#title’ => $this->t(‘Value’), ‘#size’ => 30, ‘#default_value’ => $this->value, ]; } } $no_operator = TRUE tells Views that we aren’t serious about Drupal Development Service web site directors having an choice to pick an operator. In our case, we’ll preserve issues easy and all the time assume ‘=’. You may, in fact, permit for selection of operators in case your distant service helps it. Drupal Development Service key element right here is Drupal Development Service valueForm methodology. In it, we have to set an acceptable Kind API ingredient for Drupal Development Service ‘worth’ key of Drupal Development Service $type array handed as Drupal Development Service first argument. Drupal Development Service identify ‘worth’ is essential as Drupal Development Service base class expects this key to work. Drupal Development Service type ingredient that you just return is utilized in a few locations. It’s utilized in Drupal Development Service Views UI for when Drupal Development Service web site administrator is establishing a filter. It’s additionally used if Drupal Development Service filter is uncovered, rendered in Drupal Development Service uncovered filters type with Drupal Development Service view itself. That’s it for Drupal Development Service plugin implementation. At this level we will add Drupal Development Service filter in Drupal Development Service Views UI Drupal 10 Upkeep and Help Service undefined Drupal Development Service final step adjusts our question plugin to have the ability to deal with and make use of Drupal Development Service filter. Drupal Development Service very first thing we’ll have to do is implement an addWhere methodology on Drupal Development Service question plugin class Drupal 10 Upkeep and Help Service public operate addWhere($group, $discipline, $worth = NULL, $operator = NULL) { // Guarantee all variants of 0 are literally 0. Thus ”, 0 and NULL are all // Drupal Development Service default group. if (empty($group)) { $group = 0; } // Test for a bunch. if (!isset($this->the place[$group])) { $this->setWhereGroup(‘AND’, $group); } $this->the place[$group][‘conditions’][] = [ ‘field’ => $field, ‘value’ => $value, ‘operator’ => $operator, ]; } Right here, particularly, we will see Views’ biases to SQL rear its head. Drupal Development Service methodology identify, addWhere, is odd from our perspective of querying a distant service. There isn’t any notion of a WHERE clause current in Drupal Development Service Fitbit API. Additional, Views helps grouping filters, and logical operators inside every group. Right here once more, Drupal Development Service distant service we’re utilizing has no notion of this. It’s potential Drupal Development Service distant service your implementing does through which case Drupal Development Service flexibility Views affords is wonderful. In our case it’s overkill, however I’ve copied core Views implementation for Drupal Development Service SQL question plugin, so we’ll have the ability to deal with every little thing that Drupal Development Service Views UI permits for establishing filters. Drupal Development Service remaining step is adjusting Drupal Development Service execute methodology on our question plugin to include Drupal Development Service filter into Drupal Development Service name to Drupal Development Service Fitbit API Drupal 10 Upkeep and Help Service /** * {@inheritdoc} */ public operate execute(ViewExecutable $view) { // Clip … if (isset($this->the place)) { foreach ($this->the place as $where_group => $the place) { foreach ($the place[‘conditions’] as $situation) { // Take away dot from starting of Drupal Development Service string. $field_name = ltrim($situation[‘field’], ‘.’); $filters[$field_name] = $situation[‘value’]; } } } // We at the moment solely support uid, ignore some other filters that could be // configured. $uid = isset($filters[‘uid’]) ? $filters[‘uid’] Drupal 10 Upkeep and Help Service NULL; if ($access_tokens = $this->fitbitAccessTokenManager->loadMultipleAccessToken([$uid])) { // Question distant API and return outcomes … } } Right here, we’re looping by means of any filters which have been configured on Drupal Development Service view and grabbing their values. We then ignore some other filter that will have been configured on Drupal Development Service view, since we’re solely supporting uid for now and go it alongside to $this->fitbitAccessTokenManager->loadMultipleAccessToken([$uid]), which can restrict Drupal Development Service entry tokens we get again to simply Drupal Development Service uid set and solely present us outcomes for Drupal Development Service corresponding consumer. Usually, as was Drupal Development Service case on a current shopper venture, Drupal Development Service filters that you just arrange will truly get handed alongside in Drupal Development Service distant API request. Drupal Development Service Fitbit API is a bit odd on this regard in that almost all endpoints solely return information for a single consumer anyway, so there isn’t a filtering that is sensible. That’s it! In any case that work, we will arrange a filter by uid to restrict Drupal Development Service outcomes to a single consumer. Wrap up We did it, in the end, we’ve produced a customized Fitbit leaderboard, which could look one thing like this Drupal 10 Upkeep and Help Service undefined In fact that is simply inventory 8 with Drupal Development Service Fitbit Drupal 10 module put in and configured, however it’s Views and everyone knows how one can customise Drupal Development Service feel and look of Views, so make it fairly to your coronary heart’s content material. Whereas we have checked out plenty of code, I do not assume that any of it has been horribly sophisticated. It is largely a matter of realizing what to place the place, with a wholesome dose of planning to verify our information matches into Drupal Development Service Views paradigm correctly. In abstract, Drupal Development Service steps are Drupal 10 Upkeep and Help Service Make a plan of assault, bearing in mind Drupal Development Service information you are retrieving and Drupal Development Service means Views expects to make use of it. Create discipline handlers on your information as needed. Write distant queries to retrieve your information and retailer it in rows in Drupal Development Service view object. Write filter plugins as essential to slender Drupal Development Service outcome set. There’s plenty of work in these steps, however after operating by means of it a pair instances Drupal Development Service structure makes plenty of sense. Get Drupal Development Service code! Drupal Development Service code from this text might be present in Drupal Development Service Fitbit Drupal 10 module on Drupal 10.org. It consists of a base Drupal 10 module to deal with utility setup, authentication and entry token storage and two sub-Drupal 10 modules for Views integration. Drupal Development Service first is fitbit_views_example, which I created particularly for this text collection. You’ll discover all Drupal Development Service code we went by means of in there. Drupal Development Service different one, fitbit_views is a extra absolutely featured and barely extra advanced model, together with spanning a number of API endpoints with relationship plugins. It’s best to use fitbit_views in case your intending on utilizing this performance in your web site. Be at liberty to file points and patches! Phew, that was lots. Thanks for sticking with me by means of all of it. Particular due to Greg Dunlap for trusting me with Drupal Development Service reboot of his unique collection, which has guided me by means of my very own Views question plugin implementations. Thanks additionally to Drupal Development Service Fitbit Drupal 10 module maintainer, Matt Klein, who was sort sufficient to grant me co-maintainer rights on Drupal Development Service venture. Drupal 10 Growth and Help
Drupal 10 Help: Drupal 10 Upkeep and Help Service Constructing Views Question Plugins for 8, Half 3

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.)
Drupal 10 Help: Drupal 10 Upkeep and Help Service Constructing Views Question Plugins for 8, Half 3
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.
