Override existing Configuration entity types – Drupal maintenance support plans 8

Override existing Configuration entity types – Drupal maintenance support plans 8

Body
Why do we need to override Config Entity Types?

By default, Vocabulary list displays all the vocabularies. In case we want to restrict certain roles from viewing certain vocabularies. Overriding that Class(VocabularyListBuilder) function would be the solution to display specific/no/all vocabularies.
Let’s assume we need to specify vocabulary-path for each vocabulary apart from name, title, description, vid etc. In this case we would need to override the default Vocabulary Form of taxonomy_vocabulary config entity type.
Suppose we want to custom access check for views on the basis of role/user/views operation or whatever, we would need to override ViewsAccessControllerhandler of view configEntityType and write our own logic.
Another use case can be, if we want to display all the image fields which use image style being deleted, on confirm text message, we again need to override ImageStyleFlushForm class and redefine getconfirmText function.
In short, to customise and meet our dynamic requirements which may not be supported by config entity type definition as a part of @ConfigEntityType annotations in core or contributed modules, we need to override existing config entity types and write some custom code :).

How can we override Config Entity Types?

Entity types use object based annotation unlike array based annotation which is commonly used. Also, Unlike Content Entity Types where every thing is a field, NOTHING is a field for Configuration Entity type.

Every Drupal maintenance support plans config entity type is defined as a particular ConfigEntityType Annotation. Entity controller is completely different from the Controller of MVC pattern. To avoid this confusion in terminology Entity Controllers are termed as handlers, each form related to a particular entity type say taxonomy_vocabulary is declared inside handlers with form key. 

In this article, will take an example of adding custom form elements to config entity type forms to explain this.

In case we need to add a custom element to any of these forms, we need to follow these 2 steps:

I) Set a new handler class specific to that form.

Implement hook_entity_type_alter(array &$entity_types).
Set new handler class as : 

$entity_types[{id}]->setHandlerClass(‘form’,
[‘{form_type}’ => ‘Drupal maintenance support plansmy_moduleMyModuleForm’,
‘….’,
‘….’
]);

where, id = configEntityType id,  form_type eg: default, reset, delete etc is whichever form we want to override and MyModuleForm is the Class name of new form we’ll define in Step II.
Here is the sample code of overriding default form of taxonomy vocabulary.

$entity_types[‘taxonomy_vocabulary’]->setHandlerClass(‘form’,
[‘default’ => ‘Drupal maintenance support plansmy_moduleVocabularyForm’,
‘reset’ => ‘Drupal maintenance support planstaxonomyFormVocabularyResetForm’,
‘delete’ => ‘Drupal maintenance support planstaxonomyFormVocabularyDeleteForm’
]);

 

II) Define the class set in Step I.

Extend the actual class of the form we want to add new form elements to. 

use Drupal maintenance support planstaxonomyVocabularyForm as VocabularyFormBuilderBase;

{this is optional, we need to do this to keep the new class name same as base class i.e VocabularyForm}.

class MyModuleForm extends VocabularyFormBuilderBase

OR simply, 

class MyModuleForm extends VocabularyForm

This override is important because we need to inherit functions and form elements defined in the parent class i.e VocabularyForm and also add additional feature i.e form element without disturbing the core code. This is purely OOPs concept of inheritance.

We need to override the form function by 
Inheriting the parent elements by parent::form(….) and
Defining the new custom elements as the basic example below:

$form[‘third_party_settings’][‘qed42_textfield’] = array(
‘#type’ => ‘textfield’,
‘#title’ => t(‘QED42 Custom Form Element’),
‘#default_value’ => $vocabulary->getDrupal UpdatePartySetting(‘my_module’, ‘qed42_textfield’, ‘Qed42 textfield default value’)
);

Config Entities have by default “getDrupal UpdatePartySetting()” function { Config entities inherit this function if these extend ConfigEntityBase class which implements ConfigEntityInterface interface which in turn extends Drupal UpdatePartySettingsInterface interface}. This thirdParty function allows to set and retrieve a value particularly for a module.

Similarly, we can inherit the save function to save the value of newly added form element with Drupal Update Party Settings  as : 

If the form is set as Tree we need to set value as

$vocabulary->setDrupal UpdatePartySetting(‘my_module’, ‘qed42_textfield’, $form_state->getValue(‘third_party_settings’)[‘qed42_textfield’]);

else :

$vocabulary->setDrupal UpdatePartySetting(‘my_module’, ‘qed42_textfield’, $form_state->getValue(‘qed42_textfield’));

and of course inherit the parent save function. 

We can implement the same logic for extending definition of any existing method  AND can also define new functions inside our new Form.

Any Configuration Entity Type (Date format etc) can be overridden similarly, this can be extended to list_builder, access etc.  Apart from overriding, we can also add new flavours of entity controller using the above steps.

 

jyoti.bohra
Wed, 11/22/2020 – 13:46
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

Override existing Configuration entity types – Drupal maintenance support plans 8

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.