2

I am using the ctools plumbing to create a nodal form. This is what the relevant part of my code now looks like:

/**
 * Implements hook_entity_view_alter().
 */
function mymodule_entity_view_alter(&$build) {
  $nid = $build['body']['#object']->nid;

  // Set up ctools etc. to use modal omitted for brevity.

  $build['links']['popup']['#links']['mylink'] = array(
    'title' => 'show popup',
    'href' => 'mymodule/nojs/modals',
    'html' => TRUE,
    'attributes' => array(
      'class' => array(
        'ctools-use-modal', 
        'ctools-modal-mymodule-style',
      ),
    ),
  );
}

The nid of the node this link will be attached to is known at this point. (See first line of function.)

However, my code need to be aware of the nid at each step in the modal form.

I think it possible to do this by stashing it in a session cookie, but I would like to avoid using cookies, if possible.

Is there another way to do this?

  • You could always use a hidden form field to store the nid in. – enzipher Jun 5 '14 at 20:05
  • @enzipher How do I get the access to the form object inside hook_entity_view_alter, which is where I have access to the $nid? In the function I build the form object $nid is not available (unless it can be pulled from path, as suggested by Clive). – Free Radical Jun 6 '14 at 2:44
  • Good question.. Somehow I missed which hook you were in. Clive's answer is probably as good as it gets. – enzipher Jun 6 '14 at 5:20
4

I think the simplest way would be to change your path to incorporate the nid, e.g.

function MYMODULE_menu() {
  $items['mymodule/%ctools_js/modals/%node'] = array(
    'title' => 'Title',
    'page callback' => 'MYMODULE_callback',
    'page arguments' => array(1, 3),
    ...
  );

  return $items;
}

Change the path in the link

$build['links']['popup']['#links']['mylink'] = array(
  'title' => 'show popup',
  'href' => 'mymodule/nojs/modals/' . $nid,
  'html' => TRUE,
  'attributes' => array(
    'class' => array(
      'ctools-use-modal', 
      'ctools-modal-mymodule-style',
    ),
  ),
);

And in your callback:

function MYMODULE_callback($js, $node) {
  // $node is fully loaded, continue as normal...
}

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.