Categories
CMS Drupal Mastering Development

How to create a term reference field programmatically

I need to add a taxonomy reference field to a node bundle, programmatically. I found a couple examples here, but one actually is incorrectly titled and is about creating a from field for it, and the other is from 2012. I have tried the following, based on the couple of suggested examples I found, and […]

Categories
CMS Development Drupal

Why is my site status continuing to indicate mismatched field definitions after updating?

When I visit /admin/reports/status, I am told that a few entities have fields that need to be updated or reinstalled. I created an update hook: /** * Remove the result field from the analysis entity and reinstall */ function mymodule_update_8101(&$sandbox) { $entity_name = ‘analysis’; $old_field_name = ‘result’; $new_field_name = ‘result’; $field_definition = BaseFieldDefinition::create(‘decimal’) ->setLabel(t(‘Results’)) ->setDescription(t(‘Results.’)) […]

Categories
CMS Development Drupal

Set display options for a json string in drupal 8 BaseFieldDefinition:

I have a field that stores a json string with keys and values in the database. I wish to format this output from my field definition display option to output the json string as a list. Is it possible to achieve this via such an approach? $fields[‘charges’] = BaseFieldDefinition::create(‘string’) ->setLabel(t(‘Charges’)) ->setDescription(t(‘The events charges in Ksh.’)) […]

Categories
CMS Development Drupal

How to display date and time picker for base field on custom entity

I’ve made a base field on a custom entity with type “datetime” using “datetime_default” for the form display. It’s displaying as only a date picker, with no time component, whereas I want it to look like this: If I create a field in the GUI I can select between “date only” or “date and time”. […]

Categories
CMS Development Drupal

Adding a base field definition in an update hook – it’s not appearing

I want to add another base field to a Drupal entity. It used to be possible to just change the code for the entity definition and then update it via the Drupal console. But now it has to be done in an update hook. I’ve added the following update hook, done drush updb and the […]

Categories
CMS Development Drupal

How to change field attributes of Content Entity?

In a custom module, I have a content entity in which I have defined this field: $fields[‘formheader’] = BaseFieldDefinition::create(‘string’) ->setLabel(t(‘Header’)) ->setSettings([ ‘max_length’ => 255, ‘text_processing’ => 0, ]) ->setDefaultValue(”) ->setDisplayOptions(‘view’, [ ‘label’ => ‘above’, ‘type’ => ‘string’, ‘weight’ => $weight, ]) ->setDisplayOptions(‘form’, [ ‘type’ => ‘string_textfield’, ‘weight’ => $weight, ]) ->setDisplayConfigurable(‘form’, TRUE) ->setDisplayConfigurable(‘view’, TRUE); I’d […]