0

1-I have a node with the name of the project.

2-this node has a project_name field that generates by these Tokens

3- I have a reference field that it load an entity type in the project create the page the entity type and bundle machine name are: delay , project_delay

4- I want to use the project_name in the title of project_delay

Problem: the project_name will fill after page load and is the client side then it can't be used in the project_delay title field, I want a hook or any trick to get the project_name and use it in the project_delay title.

the project_delay in create project enter image description here

Note: I use the inline entity form, in my case I set the projct_delay to inline entity form Complex

Manage form display enter image description here

Manage display enter image description here

  • will fill after page load The most simple solution would be use JavaScript/jQuery and grab project_name field value and put it in project_delay. – No Sssweat Apr 23 at 5:14
  • @NoSssweat I try with a simple alert but it has an error, is there anything about jquery that I should consider for adding the jquery? I used asset injector. – Nasser Apr 23 at 5:19
0

You can add an entry to your theme's library

/themes/theme_name/theme_name.libraries.yml

Add

ief-js:
  version: 1.x
  js:
    js/custom.js: {}
  dependencies:
    - core/jquery

/themes/theme_name/theme_name.theme

function theme_name_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {
  if ($form_id == 'the_id_of_the_ief_form') {
    $form['#attached']['library'][] = 'theme_name/ief-js'; // attach library to form
  }
}

/themes/theme_name/js/custom.js

(function ($, Drupal) {
  Drupal.behaviors.iefJS = {
    attach: function (context, settings) {
      $('#field_id_of_project_delay', context).once('iefJS').each(function () {
        var project_name = $('#field_id_of_project_name').val(); // get val
        var project_name = Drupal.checkPlain(project_name); // sanitize
        $(this).val(project_name); // set val
      });
    }
  };
})(jQuery, Drupal);

And of course, replace theme_name with your actual theme name.

Flush cache so your changes are picked up.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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