0

I want to add in a contextual link to a block.

The block shows up in different pages, not just a specific page.

Here is my current setup

$block['content'] =array(
    '#markup' => _get_markup(),
    '#attached' => array(
        'css' => array(
            drupal_get_path('module', 'module_name') . '/css/main.css',
        ),
        'js' => array(
            drupal_get_path('module', 'module_name') . '/js/main.js'
        ),
    ),
    '#contextual_links' => array(
        'add content' => array('add/content/', array('content')),
    ),
);

Based on the documentation, this should work for drupal 7 but it does not for me.

0

To be able to add your custom contextual link, you first need to check the Contextual links module is enabled. Even though you define everything correctly, it would not show, if it is disabled. Also, you need to set context to MENU_CONTEXT_INLINE in your menu items.

/**
 * Implements hook_block_view().
 */
function example_block_view($delta = '') {
  $block = array();
  switch($delta) {
    case 'my_custom_block':
      $block['subject'] = t('Example Block');
      $block['content'] = array(
      '#markup' => _get_markup(),
      '#contextual_links' => array(
          'add content' => array('add', array('node')),
      ));
      break;
    default:
      break;
  }

  return $block;
}

/**
 * Implements hook_menu().
 */
function example_menu() {
    $items['add/%/content'] = array(
        'title' => 'Add Content',
        'type' => MENU_LOCAL_ACTION,
        'context' => MENU_CONTEXT_INLINE,
        'page callback' => 'contextual_node_page',
        'page arguments' => array(1),
        'access callback' => TRUE,
    );

    // To use local task menu items, there must be a parent page.
    $items['add'] = array(
        'title' => 'Contextual Link Page',
        'page callback' => 'contextual_node_page',
        'page arguments' => array(1),
        'access callback' => TRUE,
    );
    return $items;
}

Additional resources

|improve this answer|||||
  • The thing that i'm having a hard time understanding is the menu part. This block can technically live anywhere, why is there a fixed path, also there is several components that change this think "views" without the module "Contextual links". I found a weird hackish way to get through this but I just don't feel like its right. – Rick Aug 14 '16 at 3:58
0

Here is the solution that I ended up using. By no means, this is the definite answer to my question. Although the code below accomplishes what is being asked I would HIGHLY discourage anyone from using it unless you have to.

function hook_preprocess_links(&$variables) {
    if($variables['links']['block-configure']['href'] === 'admin/structure/block/manage/BLOCKNAME/configure'){
        $variables['links']['custom_name'] = array(
            'title' => 'TITLE',
            'href' => 'path/to/node'
        );
    }
}
|improve this answer|||||

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.