0

I want to insert a link to a ctools modal popup with a custom style (class) into the link area at the bottom of an entity (e.g. a node). I do this using hook_entity_view_alter. This is how the relevant part of my code looks like:

/**
 * Implements hook_entity_view_alter().
 */
function mymodule_entity_view_alter(&$build) {
  // Set up ctools etc. to use modal omitted for brevity.
  $build['links']['popup']['#links']['mylink']['title'] = 'show popup';
  $build['links']['popup']['#links']['mylink']['href'] = 'mymodule/nojs/modals';
  $build['links']['popup']['#links']['mylink']['html'] = TRUE;
 }

The code shown above inserts the following link in the right place:

<a href="/mymodule/nojs/modals">show popup</a>

However, this leaves out the class required to give the modal a custom style. This what I really want to have this link look like:

<a href="/mymodule/nojs/modals" class="ctools-use-modal ctools-modal-mymodule-style">show popup</a>

Question: How do I get class="ctools-use-modal ctools-modal-mymodule-style" included in this link?

1

You can add the html attributes using the attributes key which accepts an array of attributes, and add classes using the class key, which also accepts an array of classes.

/**
 * Implements hook_entity_view_alter().
 */
function mymodule_entity_view_alter(&$build) {
  // 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',
      ),
    ),
  );
}

More information about available keys, refer to theme_links() function in Drupal API.

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.