Ajax Autocomplete Inline Entity Form from Entity Reference field

Drupal Ajax fun. I have a Content Type called Newsletter that displays 10 Cards (another custom content type) via Inline Entity Form. The Card Content Type has an Entity Reference Field that looks for article nodes based on what is typed in the field. In the Card Content Type itself I got Ajax working so it prepopulates the Title, Blurb(body) and Image fields when a node is chosen in the Entity reference:

/**  * @file  * MYMODULE_card.ajax.inc  */  /**  * Implements hook_form_FORM_ID_alter().  */ function MYMODULE_card_form_card_node_form_alter(&$form, &$form_state, $form_id) {    $form['field_content'][LANGUAGE_NONE][0]['target_id']['#ajax'] = array(       'callback' => 'MYMODULE_card_overrides',       'event' => 'autocompleteSelect',       'wrapper' => 'card_form',     );   // Wrap the title field    $form['#prefix'] = '<div id="card_form">';    $form['#suffix'] = '</div>';    if (!empty($form_state['values']['field_content'][LANGUAGE_NONE][0]['target_id'])) {     // Load the node from target_id.     unset($form_state['input']['title']);     unset($form_state['input']['field_blurb_override']);     unset($form_state['input']['field_image_override']);     unset($form_state['input']['field_link_override']);  $target_node = node_load($form_state['values']['field_content'][LANGUAGE_NONE][0]['target_id']); $form['title']['#default_value'] = $target_node->title;  // Set Blurb.     $blurb = '';     if (!empty($target_node->body[LANGUAGE_NONE][0]['summary'])){       $blurb = $target_node->body[LANGUAGE_NONE][0]['summary'];     }     $form['field_blurb_override'][LANGUAGE_NONE][0]['#default_value'] = $blurb;      // Set image.     $form['field_image_override'][LANGUAGE_NONE][0]['#default_value']['fid'] = $target_node->field_image_homepage_story[LANGUAGE_NONE][0]['fid'];      // Set link.     // TODO: use url() with ABSOLUTE once we get that working again.     $link_value = 'MYSITE' . drupal_get_path_alias('node/' . $target_node->nid);      $form['field_link_override'][LANGUAGE_NONE][0]['#default_value']['url'] = $link_value;   }     dpm($form,'form card');   dpm($form_state,'form_state card');   return $form; }  function MYMODULE_card_overrides(&$form, $form_state) {    return $form; } 

This works great on its own, but in the IEF fails cause, hey different form. So I am trying to get the ajax to work when Card is used as an IEF in the Newsletter CT. The code below uses what I found at this most excellent post https://www.drupal.org/node/1757410 to make 10 IEFs and open them up for editing. I also use it to put in an AJAX callback per IEF. But now am stuck on what to modify in the form_state and what to return from the form in MYMODULE_newsletter_overrides to get it to work like the card does on its own. As you can see I have even tried ajax_command_invoke to get this working as well, but no dice. Any pointers would be greatly appreciated!

function MYMODULE_newsletter_field_widget_form_alter(&$element, &$form_state, $context) {   global $user;    if (isset($element['#field_name']) && 'field_article_card' == $element['#field_name']) {      ////dpm($element, 'element');     //dpm($form_state, 'form_state');     //dpm($context, 'context');     // Do it only where no default value is set     $entityList = array();     if (!isset($element['entities'][0])) {       $delta = 0;       // Starting with new set of empty cards       //$cloneList = array(...); // Put here nids you want to prepopulate with        for ($i = 0; $i < 10; $i++) {         $newEntity = entity_create('node', array('type' => 'card'));         $newEntity->uid = $user->uid;         $newEntity->title = 'Article Card ' . $i;         $entityList[$i] = $newEntity;       }     }     // In the "invisible" form, find out the part that really helps us here     foreach ($form_state['inline_entity_form'] as $keyState => $formStateItem) {       if ('card' == $formStateItem['settings']['bundles'][0]) {         break;       }     }      // Setup bundle for auto open each form     $settings = inline_entity_form_settings($context['field'], $context['instance']);     $bundle = reset($settings['create_bundles']);     $controller = inline_entity_form_get_controller($context['instance']);     $parents = array_merge($element['#field_parents'], array($element['#field_name'], $element['#language']));     $parent_langcode = entity_language($element['#entity_type'], $element['#entity']);       // Modify the visible part      //dpm($entityList, 'entityList');     //$entityList = entity_load('node', $cloneList);     foreach ($entityList as $delta => $entity) {       // The whole list can be found in inline_entity_form.module, around line 350        $element['entities'][$delta] = array();       $element['entities'][$delta]['#entity'] = $entity;       $element['entities'][$delta]['#needs_save'] = TRUE;       $element['entities'][$delta]['#weight'] = 0;        $element['entities'][$delta]['delta'] = array(         '#type' => 'value',         '#value' => $delta,       );       // Add the appropriate form.       $element['entities'][$delta]['form'] = array(         '#type' => 'container',         '#attributes' => array('class' => array('ief-form', 'ief-form-row')),         '#op' => 'edit',         '#parents' => array_merge($parents, array('entities', $delta, 'form')),         '#entity' => $entity,         '#entity_type' => $settings['entity_type'],         '#parent_language' => $parent_langcode,         '#ief_id' => $keyState,         '#ief_row_delta' => $delta,       );       $element['entities'][$delta]['form'] += inline_entity_form_entity_form($controller, $element['entities'][$delta]['form'], $form_state);        $form_state['inline_entity_form'][$keyState]['entities'][$delta] = array(         'entity' => $entity,         'weight' => 0,         'form' => 'add',         'form settings' => array(           'bundle' => $bundle,         ),         'needs_save' => FALSE,       );        // Add ajax form autocomplete to each ief       $card_form_id = 'card_form_' . $delta;       $element['entities'][$delta]['form']['field_content'][LANGUAGE_NONE][0]['target_id']['#ajax'] = array(         'callback' => 'MYMODULE_newsletter_overrides',         'event' => 'autocompleteSelect',         'wrapper' => $card_form_id,       );       // Wrap the ief       $element['entities'][$delta]['form']['#prefix'] = '<div id="' . $card_form_id . '">';       $element['entities'][$delta]['form']['#suffix'] = '</div>';        //dpm($form_state['inline_entity_form'][$keyState], 'form state entities');       //dpm($element,'element');     }   } }  function MYMODULE_newsletter_overrides(&$form, $form_state) {   //dpm($form_state, 'formstate overrides');   //dpm($form, 'form overrides');    $triggering_element = $form_state['triggering_element'];   $triggering_field_name = $triggering_element['#field_name'];   $parent = $triggering_element['#parents'][0];   $parent_id = $triggering_element['#parents'][3];    $target_node = node_load($triggering_element['#entity']->field_content[LANGUAGE_NONE][0]['target_id']);    // Assign fields to address.   $title_field = 'input[name="' . $parent . '[' . LANGUAGE_NONE . '][entities][' . $parent_id . '][form][title][' . LANGUAGE_NONE . '][0][value]"]';   $blurb_field = 'textarea[name="' . $parent . '[' . LANGUAGE_NONE . '][entities][' . $parent_id . '][form][field_blurb_override][' . LANGUAGE_NONE . '][0][value]"]';   $image_field = 'input[name="' . $parent . '[' . LANGUAGE_NONE . '][entities][' . $parent_id . '][form][field_image_override][' . LANGUAGE_NONE . '][0][fid]"]';    // Get values from target node.   $promo_title = $target_node->field_promo_title[LANGUAGE_NONE][0]['value'];   $card_title = empty($promo_title) ? $target_node->title : $promo_title;   $card_blurb = $target_node->body[LANGUAGE_NONE][0]['summary'];   $card_image_fid = $target_node->field_image_homepage_story[LANGUAGE_NONE][0]['fid'];    $commands[] = ajax_command_invoke($title_field, 'val', array($card_title));   $commands[] = ajax_command_invoke($blurb_field, 'val', array($card_blurb));   $commands[] = ajax_command_invoke($image_field, 'val', array($card_image_fid));    $path = libraries_get_path('FirePHPCore');   if (file_exists($path . '/fb.php')) {     include_once $path . '/fb.php';     include_once $path . '/FirePHP.class.php';      dfb($form_state, 'MYMODULE_newsletter_overrides: $form_state', FirePHP::WARN);     dfb($form, 'MYMODULE_newsletter_overrides: $form', FirePHP::WARN);     dfb($form_state['triggering_element'],'MYMODULE_newsletter_overrides: $form_state triggering_element', FirePHP::WARN);     dfb($triggering_element['#entity']->field_content[LANGUAGE_NONE][0]['target_id'],'MYMODULE_newsletter_overrides: target_id', FirePHP::WARN);   }   //  return array( //    '#type' => 'ajax', //    '#commands' => $commands, //  );   return $form['field_article_card'][LANGUAGE_NONE]['entities'][$parent_id];  } 

Replay

Category: ajax Time: 2016-07-28 Views: 0

Related post

iOS development

Android development

Python development

JAVA development

Development language

PHP development

Ruby development

search

Front-end development

Database

development tools

Open Platform

Javascript development

.NET development

cloud computing

server

Copyright (C) avrocks.com, All Rights Reserved.

processed in 0.124 (s). 12 q(s)