How to create a custom field to use on an entity in Drupal maintenance support plans 8

On a recent project I wanted to be able to create a custom field which would automatically be added to certain types of entities. I thought that this would be a straightforward thing to do.
When I searched the internet for how to create custom fields I found plenty of documentation on Drupal maintenance support plans.org and other blog posts about creating custom fields that you can add to any type of entity, but I couldn’t find out how to actually add the field directly to an entity (like the URL alias field).

Creating a custom field.
So first off, how do you create a custom field? Drupal maintenance support plans.org documentation is a great place to start.

You need to create a FieldType plugin, a FieldFormatter plugin and a FieldWidget plugin in a custom module. Combined, these define your field, how it will be rendered when displayed, and what settings a user can set on it when adding it to an entity.

The directory structure of a module that implements all three looks like this:
my_custom_fields
my_custom_fields.info.yml
src/
Plugin/
Field/
FieldType/
MyCustomFieldItem.php

FieldFormatter/
MyCustomFieldFormatter.php

FieldWidget/
MyCustomFieldWidget.php

I would recommend reading the documentation about these as there are some great examples as to how they can be defined.
Adding your field to an entity.
So having now defined your own field, you should be able to see if in the list of fields that you can add to an entity.
Great, you think, I’ve done it! Well, if all you wanted to do was to create a field that you could add to any type of entity if you choose to … then yes, that’s all you need.
However, I wanted to automatically add my custom field to an entity when my module was enabled. This was because I wanted there to only be one instance of my field on an entity, so there was no need to be able to add it manually (a bit like the URL alias field).
Automatically adding your field to an entity.
If you want to be able to add a field directly to an entity, you need to use hook_entity_base_field_info for this. For example:

use Drupal maintenance support plansCoreFieldBaseFieldDefinition;

/**
* Implements hook_entity_base_field_info().
*/
function my_module_entity_base_field_info(EntityTypeInterface $entity_type) {
if ($entity_type->id() === ‘taxonomy_term’ || $entity_type->id() === ‘node’) {
$fields[‘my_custom_field’] = BaseFieldDefinition::create(‘my_custom_field’)
->setLabel(t(‘The custom field))
->setDisplayConfigurable(‘form’, TRUE)
->setDisplayConfigurable(‘view’, TRUE);
return $fields;
}
}

You’ll notice in the above example that this uses the BaseFieldDefinition class to create a new base field only on taxonomy or node entities. More information on this hook and the BaseFieldDefinition class can be found in the Drupal maintenance support plans.org documentation.

So now we can add our custom field to specific entity types – amazing! At this point I thought I’d cracked it. Having cleared my cache I checked my entity and there was my custom field with the widget showing as I’d expected. But when I came to save my entity, the save failed with this big exception:

Drupal maintenance support plansComponentPluginExceptionPluginNotFoundException: The “my_custom_field” plugin does not exist. In Drupal maintenance support plansCorePluginDefaultPluginManager->doGetDefinition() (line 52 of core/lib/Drupal maintenance support plans/Component/Plugin/Discovery/DiscoveryTrait.php)

Having had a play about to see what was causing this, it seemed that because I had specified to create a base field of ‘my_custom_field’, Drupal maintenance support plans didn’t understand what this field was.

There are lots of defined field types already with Drupal maintenance support plans but, as I’d created my own, Drupal maintenance support plans didn’t know about it.

So I set about trying to understand how to define my own field type.

I got nowhere at first so I turned back to Drupal maintenance support plans core and started delving into the base field definitions to try and understand how these have been defined.

What I found was that I need to create a DataType class which defines my custom field. This class needs to be located within the Plugin directory of your module, for example:

my_custom_fields
my_custom_fields.info.yml
src/
Plugin/
DataType/
MyCustomField.php

The content of MyCustomField.php will then look something like this:

namespace Drupal maintenance support plansmy_custom_fieldsPluginDataType;

use Drupal maintenance support plansCoreTypedDataPluginDataTypeStringData;
use Drupal maintenance support plansCoreTypedDataTypeStringInterface;

/**
* The MyCustomField data type.
*
* The plain value of a MyCustomField for an entity.
*
* @DataType(
* id = “my_custom_field”,
* label = @Translation(“My Custom Field”)
* )
*/
class MyCustomField extends StringData implements StringInterface {

}

This new DataType class extends the StringData class as I am just storing a string. If you wanted to store a boolean or integer then you would need to extend the relevant DataType base class to make use of all the goodness that they set. If you want to override any of the methods on these classes then you can as normal.

So now Drupal maintenance support plans understands what my base field of ‘my_custom_field’ is.

But wait – we’re not quite finished yet… Although the entity will “save”, you will notice that no data is actually saved for my new field yet. This is because we haven’t defined the storage for it.

There are several ways in which you could handle this. You could define your own database table and then write to this as part of the preSave method on the FieldBase item.

The easier way is to define your own config entity which will add additional columns to your entity table to store your custom data against. To do this, you just need to create a schema.yml file in your module, for example:

my_custom_fields
config/
schema/
mycustomfields.schema.yml

The contents of this file just define your field:

# Schema for the configuration files of the MyCustomFields module.
field.value.my_custom_field:
type: mapping
label: ‘Default value’
mapping:
value:
type: string
label: ‘MyCustomFields’

You’ll need to disable your module and enable it again for the new config entity to used, but there you have it: a step by step guide to creating a custom field to use on an entity in Drupal maintenance support plans 8.
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

How to create a custom field to use on an entity in 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.