2

I have a content type "consultation" that has an inline entity reference to 1 node of a content type "prescription". This content type prescription has multiple inline entity references to the content type "medecine".

I'm using the module Inline Entity Form so I create/add all of these nodes in the creation form of "consultation".

I'm trying to change the text "Add new node" in the form to something else. I know how to do this when it's a direct entity reference, but how do I get to the "Add new medecine" for "medecine"?

This is my code so far:

function node_change_form_alter(&$form, $form_state, $form_id) {
switch ($form_id) {
case 'consultation_node_form':
  $form['field_prescription_med']['und']['actions']['ief_add']['#value'] = t('Add new medecine');
  $form['field_prescription_med']['und']['form']['#title'] = t('Add new medecine');
  $form['field_prescription_med']['und']['form']['actions']['ief_add_save']['#value'] = t('Add new medecine');
  break;
}}

In this code:

  • field_prescription is the name of the entity reference field in "consultation" (to a "prescription")
  • field_prescription_med is the name of the entity reference field in "prescription" (to "medecine")

1 Answer 1

1

You can use hook_field_widget_WIDGET_TYPE_form_alter() to alter the button name

example:

/**
 * Implements hook_field_widget_WIDGET_TYPE_form_alter().
 */
function example_field_widget_inline_entity_form_form_alter(&$element, &$form_state, $context) {
  // Rename the button in specific form.
  if($form_state['build_info']['form_id'] == 'eck__entity__form_edit_sample_sample') {
    $element['actions']['ief_add']['#value'] = t('Add new medecine');
  }
}

enter image description here

3
  • What formid should I use in my case? I thought it'd be consultation_node_fo‌​rm, but that doesn't seem to change anything (nor does prescription_node_fo‌​rm). The heading I use is: function my_module_inline_entity_form_form_alter(&$element, &$form_state, $context) which is correct I assume? Aug 17, 2016 at 9:07
  • Ah, it was function my_module_field_widget_inline_ent‌​ity_form_form_alter Aug 17, 2016 at 9:12
  • you can check the value of your form id by dumping $form_state['build_info']['form_id'], inside your function print "<pre>" . var_dump($form_state['build_info']['form_id']) . "</pre>"; or if you have devel module installed in your drupal project you can use dpm($form_state['build_info']['form_id'])
    – iamroald
    Aug 17, 2016 at 9:13

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.