4

I will try to be as specific as possible.

I have a required text plain field in a content type, say 'field_A'. This will have values of minimum 2 words, maximum 5 words. Words will be separated by spaces. I can be certain of this.

What I want, is to have another field, say 'field_B' in which I want to keep the last word of field_A. I would like this to happen automatically (upon creation of a node of this type).

I have no idea if this is possible ...

So, say I 'm adding new content of this type, and use value "lorem ipsum whatever" for field_A, upon submitting I would like the word 'whatever' to be inserted as a value in field_B.

I 'd be grateful of any help .. I couldn't find relevant info on this, it's kinda hard to describe in a few words.

Working on D8.

1 Answer 1

3

You probably need a custom module for this.

Add MODULENAME directory inside your modules directory. Inside the directroy create file MODULENAME.info.yml with contents:

name: Last word copy
description: 'gets last word from field_a and saves it to field_b'
core: 8.x
type: module
package: COMPANYNAME
dependencies:
  - node

This module implements hook_form_alter that will make field_b readonly / disabled and hook_entity_presave which will get the last word added to field_b upon save or update of a node of CONTENTTYPE

Create also MODULENAME.module file with contents:

<?php
function MODULENAME_form_alter(&$form, &$form_state, $form_id) { 
  if(in_array($form_id,['node_CONTENTTYPE_form','node_CONTENTTYPE_edit_form'])) {
    $form['field_b']['#disabled'] = 'disabled';
  }   
} 

function MODULENAME_entity_presave(Drupal\Core\Entity\EntityInterface $entity) {  
  if($entity->bundle() == "CONTENTTYPE"){    
    $pieces = explode(' ', $entity->field_a->value);
    $last_word = array_pop($pieces);
    $entity->field_b->value = $last_word;
    //save is done automatically
  }  
}

You NEED to replace MODULENAME and CONTENTTYPE in the above code.

If all is correct go to Extend (admin/modules) and enable the module.

8
  • thanks a lot for your info. I haven't created any custom modules yet - I 'm fairly new to drupal. But I will look into that. I thought it might be possible through preprocess functions, or any other (fairly simple) way. But still, your contribution is invaluable.
    – thomas
    Commented Apr 4, 2018 at 11:46
  • Added more specific instructions on how to create this module
    – GiorgosK
    Commented Apr 4, 2018 at 12:15
  • It does work my friend. I just tried it. Really helpful, and very well explained and given. Thanks a lot. I will research these hook functions, they can clearly be extremely useful .. If you can point to any good starting point for documentation and/or tutorials, please do.
    – thomas
    Commented Apr 4, 2018 at 19:42
  • 1
    In Drupal 8 You can exclude the form_alter() hook and just remove the field from the form display in the UI under Manage form display for the entity type.
    – sonfd
    Commented Apr 4, 2018 at 20:35
  • 1
    title/label has special way to get the value $entity->label() ... perhaps you should spend some time setting up a development environment for yourself drupal.stackexchange.com/questions/259037/…
    – GiorgosK
    Commented Apr 5, 2018 at 10:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.