Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By kunalkursija on
Hello,
I have a requirement, which I know how to achieve in Drupal 7. However, I am not sure of the D8 approach. Below mentioned are the details.
I have following available in my Drupal UI:
- Article Content Type
- Tags field, Which is provided by default as an entity reference of Taxanomy Term.
Requirement:
- Create checkbox element inside the "Edit Form" of Tags field (i.e: field_config_edit_form).
- Save this checkbox element value.
Drupal 7 Approach:
- Alter the field edit form.
- Add the form element as following:
$form['instance']['test_field_setting'] = array( '#type' => 'checkbox', '#title' => t('Test Field Setting'), '#weight' => -7, );
- Thats It, Anything mentioned inside
$form['instance']
array would used to get saved by Drupal in field_config table.
Drupal 8 Approach: I am not sure about this
- Alter the form with form ID "
field_config_edit_form
" - Add the form element as following:
$form['test_field_setting'] = array( '#type' => 'checkbox', '#title' => t('Test Field Setting'), '#weight' => -7, );
- Add the custom submit handler.
- Inside Submit handler, Write code that will how load the "field config" for "Tags Field" and set values for your element inside that config.
I am getting a feeling that this might not be the correct approach to achieve this.
Please let me know if you know a better way or if my approach is not ideal w.r.t Drupal standards.