Aten Design Group Drupal 10 Upkeep and Assist Service Utilizing Tackle Fields in Configuration Types

In 7, Drupal Development Tackle Discipline Drupal 10 module supplied builders a simple technique to accumulate complicated handle info with relative ease. You can merely add Drupal Development subject to your content material kind and configure which international locations you support together with what components of an handle are wanted. Nevertheless, this ease was restricted to fieldable entities. In the event you wanted to gather handle info someplace that wasn’t a fieldable entity, you had much more work in retailer for you. Likelihood is good that Drupal Development finish end result could be as few textual content fields as potential, no validation, and solely supporting with a single nation. In the event you had been feeling bold, perhaps you’d have supplied a choose listing with Drupal Development states or provinces supplied by way of a hardcoded array. Throughout my most up-to-date 8 undertaking I wished to gather structured handle info exterior Drupal Development context of an entity. Particularly, I wished so as to add a piece for handle and cellphone quantity to Drupal Development Primary Web site Settings configuration web page. Because it seems, Drupal Development similar performance you get on entities is now additionally obtainable to Drupal Development Type API. Tackle Discipline’s port to 8 got here in Drupal Development type of an entire new Drupal 10 module, Drupal Development Tackle Drupal 10 module. With it comes a brand new handle type component. Let’s use that so as to add a “Web site Tackle” subject to Drupal Development Primary Settings. First we’ll implement hook_form_FORM_ID_alter() in a customized Drupal 10 module’s .Drupal 10 module file Drupal 10 Upkeep and Assist Service use CoreFormFormStateInterface;   operate MYMODULE_form_system_site_information_settings_alter(&$type, FormStateInterface $form_state) { // Overrides go right here… } Don’t overlook so as to add use CoreFormFormStateInterface; at Drupal Development high of your file. Subsequent, we’ll add a particulars group and a fieldset for Drupal Development handle elements to enter Drupal 10 Upkeep and Assist Service operate MYMODULE_form_system_site_information_settings_alter(&$type, FormStateInterface $form_state) { // Create our contact info part. $type[‘site_location’] = [ ‘#type’ => ‘details’, ‘#title’ => t(‘Site Location’), ‘#open’ => TRUE, ];   $type[‘site_location’][‘address’] = [ ‘#type’ => ‘fieldset’, ‘#title’ => t(‘Address’), ]; } As soon as Drupal Development fieldset is in place, we are able to go forward and add Drupal Development handle elements. To do this you’ll first want to put in Drupal Development Tackle Drupal 10 module and its dependencies. You’ll additionally want so as to add use CommerceGuysAddressingAddressFormatAddressField; at Drupal Development high of Drupal Development file as we’ll want a few of Drupal Development constants outlined there later. use CoreFormFormStateInterface; use CommerceGuysAddressingAddressFormatAddressField;   operate MYMODULE_form_system_site_information_settings_alter(&$type, FormStateInterface $form_state) { // … element and fieldset code …   // Create Drupal Development handle subject. $type[‘site_location’][‘address’][‘site_address’] = [ ‘#type’ => ‘address’, ‘#default_value’ => [‘country_code’ => ‘US’], ‘#used_fields’ => [ AddressField Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServiceADDRESS_LINE1, AddressField Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServiceADDRESS_LINE2, AddressField Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServiceADMINISTRATIVE_AREA, AddressField Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServiceLOCALITY, AddressField Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServicePOSTAL_CODE, ], ‘#available_countries’ => [‘US’], ]; } There’s a couple of issues we’re doing right here price going over. First we set ‘#kind’ => ‘handle’, which Drupal Development Tackle Drupal 10 module creates for us. Subsequent we set a #default_value for country_code to US. That means Drupal Development United States particular subject config is displayed when Drupal Development web page masses. Drupal Development Company #used_fields key permits us to configure which handle info we need to accumulate. That is executed by passing an array of constants as outlined in Drupal Development AddressField class. Drupal Development Company full listing of choices is Drupal 10 Upkeep and Assist Service AddressField Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist ServiceADMINISTRATIVE_AREA AddressField Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist ServiceLOCALITY AddressField Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist ServiceDEPENDENT_LOCALITY AddressField Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist ServicePOSTAL_CODE AddressField Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist ServiceSORTING_CODE AddressField Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist ServiceADDRESS_LINE1 AddressField Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist ServiceADDRESS_LINE2 AddressField Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist ServiceORGANIZATION AddressField Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist ServiceGIVEN_NAME AddressField Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist ServiceADDITIONAL_NAME AddressField Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist ServiceFAMILY_NAME With none configuration, a full handle subject seems to be like this when displaying addresses for Drupal Development United States. For our instance above, we solely wanted Drupal Development road handle (ADDRESS_LINE1 and ADDRESS_LINE2), metropolis (LOCALITY), state (ADMINISTRATIVE_AREA), and zip code (POSTAL_CODE). Lastly, we outline which international locations we will likely be supporting. That is executed by passing an array of nation codes into Drupal Development #available_countries key. For our instance we solely want addresses from Drupal Development United States, in order that’s Drupal Development solely worth we cross in. Drupal Development Company final step in our course of is saving Drupal Development info to Drupal Development Primary Web site Settings config file. First we have to add a brand new submit handler to Drupal Development type. At Drupal Development finish of our hook, let’s add this Drupal 10 Upkeep and Assist Service operate MYMODULE_form_system_site_information_settings_alter(&$type, FormStateInterface $form_state) { // … element and fieldset code …   // … handle subject code …   // Add a customized submit handler for our new values. $type[‘#submit’][] = ‘MYMODULE_site_address_submit’; } Now we’ll create Drupal Development handler Drupal 10 Upkeep and Assist Service /** * Customized submit handler for our handle settings. */ operate MYMODULE_site_address_submit($type, FormStateInterface $form_state) { Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist ServiceconfigFactory()->getEditable(‘system.website’) ->set(‘handle’, $form_state->getValue(‘site_address’)) ->save(); } This masses our site_address subject from Drupal Development submitted values in $form_state, and saves it to Drupal Development system.website config. Drupal Development Company exported system.website.yml file ought to now look one thing like Drupal 10 Upkeep and Assist Service identify Drupal 10 Upkeep and Assist Service ‘My Superior Web site’ mail Drupal 10 Upkeep and Assist Service check@area.com slogan Drupal 10 Upkeep and Assist Service ” web page Drupal 10 Upkeep and Assist Service 403 Drupal 10 Upkeep and Assist Service ” 404 Drupal 10 Upkeep and Assist Service ” entrance Drupal 10 Upkeep and Assist Service /consumer/login admin_compact_mode Drupal 10 Upkeep and Assist Service false weight_select_max Drupal 10 Upkeep and Assist Service 100 langcode Drupal 10 Upkeep and Assist Service en default_langcode Drupal 10 Upkeep and Assist Service en handle Drupal 10 Upkeep and Assist Service country_code Drupal 10 Upkeep and Assist Service US langcode Drupal 10 Upkeep and Assist Service ” address_line1 Drupal 10 Upkeep and Assist Service ‘123 W Elm St.’ address_line2 Drupal 10 Upkeep and Assist Service ” locality Drupal 10 Upkeep and Assist Service Denver administrative_area Drupal 10 Upkeep and Assist Service CO postal_code Drupal 10 Upkeep and Assist Service ‘80266’ given_name Drupal 10 Upkeep and Assist Service null additional_name Drupal 10 Upkeep and Assist Service null family_name Drupal 10 Upkeep and Assist Service null group Drupal 10 Upkeep and Assist Service null sorting_code Drupal 10 Upkeep and Assist Service null dependent_locality Drupal 10 Upkeep and Assist Service null After that, we want to ensure our subject will use Drupal Development saved handle as Drupal Development #default_value. Again in our hook, let’s replace that key with Drupal Development following Drupal 10 Upkeep and Assist Service operate MYMODULE_form_system_site_information_settings_alter(&$type, FormStateInterface $form_state) { // … element and fieldset code …   // Create Drupal Development handle subject. $type[‘site_location’][‘address’][‘site_address’] = [ ‘#type’ => ‘address’, ‘#default_value’ => Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Serviceconfig(‘system.site’)->get(‘address’) ?? [ ‘country_code’ => ‘US’, ], ‘#used_fields’ => [ AddressField Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServiceADDRESS_LINE1, AddressField Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServiceADDRESS_LINE2, AddressField Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServiceADMINISTRATIVE_AREA, AddressField Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServiceLOCALITY, AddressField Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServicePOSTAL_CODE, ], ‘#available_countries’ => [‘US’], ];   // … customized submit handler … } Utilizing PHP 7’s null coalesce operator, we both set Drupal Development default to Drupal Development saved values or to a smart fallback if nothing has been saved but. Placing this all collectively, our Drupal 10 module file ought to now appear like this Drupal 10 Upkeep and Assist Service <?php   /** * @file * Fundamental Drupal 10 module file. */   use CoreFormFormStateInterface; use CommerceGuysAddressingAddressFormatAddressField;   /** * Implements hook_form_ID_alter(). */ operate MYMODULE_form_system_site_information_settings_alter(&$type, FormStateInterface $form_state) { // Create our contact info part. $type[‘site_location’] = [ ‘#type’ => ‘details’, ‘#title’ => t(‘Site Location’), ‘#open’ => TRUE, ];   $type[‘site_location’][‘address’] = [ ‘#type’ => ‘fieldset’, ‘#title’ => t(‘Address’), ];   // Create Drupal Development handle subject. $type[‘site_location’][‘address’][‘site_address’] = [ ‘#type’ => ‘address’, ‘#default_value’ => Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support Serviceconfig(‘system.site’)->get(‘address’) ?? [ ‘country_code’ => ‘US’, ], ‘#used_fields’ => [ AddressField Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServiceADDRESS_LINE1, AddressField Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServiceADDRESS_LINE2, AddressField Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServiceADMINISTRATIVE_AREA, AddressField Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServiceLOCALITY, AddressField Drupal 10 Maintenance and Support Service Drupal 10 Maintenance and Support ServicePOSTAL_CODE, ], ‘#available_countries’ => [‘US’], ];   // Add a customized submit handler for our new values. $type[‘#submit’][] = ‘MYMODULE_site_address_submit’; }   /** * Customized submit handler for our handle settings. */ operate MYMODULE_site_address_submit($type, FormStateInterface $form_state) { Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist ServiceconfigFactory()->getEditable(‘system.website’) ->set(‘handle’, $form_state->getValue(‘site_address’)) ->save(); } Lastly we must always do some home cleansing in case our Drupal 10 module will get uninstalled for any purpose. In Drupal Development similar listing as Drupal Development MYMODULE.Drupal 10 module file, let’s add a MYMODULE.set up file with Drupal Development following code Drupal 10 Upkeep and Assist Service /** * Implements hook_uninstall(). */ operate MYMODULE_uninstall() { // Delete Drupal Development customized handle config values. Drupal 10 Upkeep and Assist Service Drupal 10 Upkeep and Assist ServiceconfigFactory()->getEditable(‘system.website’) ->clear(‘handle’) ->save(); } That’s it! Now we’ve got a means to supply location info to Drupal Development international website configuration. Utilizing that information, I’ll be capable to show this info elsewhere as textual content or as a Google Map. With the ability to use Drupal Development similar options that Tackle subject sorts have, I can leverage different Drupal 10 modules that show handle info or construct my very own shows, as a result of I now have reliably structured information to work with. Drupal 10 Improvement and Assist

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

Aten Design Group Drupal 10 Upkeep and Assist Service Utilizing Tackle Fields in Configuration Types

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.