0

I use extensively ctools modal for entity creation/editing. I'm now integrating an ECK entity but the behavior is not consistent with other entity types (node, group, etc.)

Typically when saving an entity in a ctools modal the form state is altered and I check with something like if (!empty($form_state['executed']) && empty($form_state['rebuild'])) to see if the form has been submitted. With this information I can close the modal and replace some elements on the page. However, with this ECK entity, $form_state['executed'] and $form_state['rebuild'] are exactly the same before and after form submission.

Of note is that, for now, I've commented out the built-in ECK redirect logic - that's another issue I'll have to come back to. In eck__entity__form_submit() I've commented out $state['redirect'] = $uri['path']; as a redirect is not what I wanted.

With the commented out redirect, the form submission works: An entity is created, the content within the modal is updated to show a success message (message is generated by ECK module).

Here is the code within the page callback that displays the modal:

$form = eck__entity__add('external_user', 'external_user');
$title = t('Add external speaker');

//  $commands = array();

ctools_include('modal');
ctools_include('ajax');
// You can ignore some things, like this conditional, I've only done it for debugging purposes
if (!isset($form_state)) {
  $form_state = array(
    'title' => $title,
    'ajax'  => TRUE,
    'build_info' => array(
      'args'  => [$form['entity']['#value']]
    ),
  );

  $commands = ctools_modal_form_wrapper($form['#form_id'], $form_state);
}

if (!empty($form_state['executed']) && empty($form_state['rebuild'])) {
  // overwrite commands.
  $commands = array();

  ctools_add_js('ajax-responder');
  $commands[] = ctools_modal_command_dismiss();
  $event_node = (object) [
    'uid' => $user->uid,
    'name' => (isset($user->name) ? $user->name : ''),
    'type' => 'event',
    'language' => LANGUAGE_NONE,
    'status' => 1,
  ];
  $event_form = drupal_get_form('event_node_form', $event_node);
  $field = $event_form['og_group_ref'];
  $field['#attributes']['class'][] = 'selectpicker';
  $field[LANGUAGE_NONE][0]['default']['#value'] = [$form_state['build_info']['args'][0]->nid];
  $commands[] = ajax_command_replace('[id^="edit-field-external-speaker-und"]', drupal_render($field));
}

print ajax_render($commands);
exit;

If I get hackish with it, I can use if (stripos($commands[0]['output'], 'Status message') !== FALSE) { as my conditional and all works as expected.

The main question is, why is the $form_state not being updated in the way I expect and is typical of other entity forms within a ctools modal?

  • My guess would be because ECK doesn't generate entities in the same way that defining it in code would with the API? – Kevin Jan 3 at 16:32

Your Answer

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

Browse other questions tagged or ask your own question.