3

In a Module list page (admin/modules) , We could see help, permission or configure links in Operations column. and also Module Filter customize it as a dropdown for each module listed.

Now I need to create a new link in that operations for every module installed. How should I do it?

I think I should go with hook_page_alter() but don't know how to start with since the $page array of admin/modules page is too long.

0

I think not just form_alter makes this happen, "theme" too plays a game here. When you look into coder review Module file, you could find that.

Code Review which is a submodule of Coder Module also adds a link to the system_modules form.

function coder_review_form_system_modules_alter(&$form, &$form_state, $form_id) {
  if (!user_access('view code review') || isset($form['confirm'])) {
    return;
  }

  $path = drupal_get_path('module', 'coder_review');
  $form['#attached']['css'][] = "$path/coder_review.css";
  foreach ($form['modules'] as $package_name => &$package) {
    if ($package_name[0] != '#') {
      foreach ($package as $module_name => &$module) {
        if ($module_name[0] != '#') {
          $module['links']['coder_review'] = array(
            '#type' => 'link',
            '#title' => t('Code review'),
            '#href' => "admin/config/development/coder/review/$module_name",
            '#options' => array('attributes' => array('class' => array('module-link', 'module-link-coder-review'), 'title' => t('Coder review'))),
          );
          $package['#theme'] = 'coder_review_modules_fieldset';
        }
      }
    }
  }
}

There is a particular line in this code that needs to be noted :

$package['#theme'] = 'coder_review_modules_fieldset';

This might be the reason your not able to get the link.

Function theme_coder_review_modules_fieldset() is implemented in coder_review.admin.inc file. In its comment block you could also find something like this

/**
 * Returns HTML for the modules form.
 *
 * This was copied from theme_system_modules_fieldset() and modified to handle
 * additional links.
 *
 * @param array $variables
 *   An associative array containing the key:
 *   - form: A render element representing the form.
 *
 * @ingroup themeable
 *
 * @todo Create an issue for this and get this function into D8.
 */
2

The system_modules() form is the one that provides that module list.

You can form alter that in a custom module like so:

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MODULE_NAME_form_system_modules_alter(&$form, &$form_state, $form_id) {
  // Loop through the array of module packages.
  foreach (element_children($form['modules']) as $package) {
    // Loop through the array of module rows for each package.
    foreach (element_children($form['modules'][$package]) as $module_filename) {
      // Add an extra operation link for each module row.
      $form['modules'][$package][$module_filename]['links']['new_operation_link'] = array(
        '#type' => 'link',
        '#title' => t('Link text'),
        '#href' => 'link/url',
        '#options' => array(
          'attributes' => array(
            'class' => array('module-link', 'module-link-configure'),
            'title' => t('Link hover text'),
          ),
        ),
      );
    }
  }
}

Note that you need to change MODULE_NAME to the name of your module and you have the change the link key, text, url, etc. to meet your requirements.

[EDIT]
I have now tested it and it works. Here is a working example with the placeholder values changed to make a link to google.

/**
 * Implements hook_form_FORM_ID_alter().
 */
function MODULE_NAME_form_system_modules_alter(&$form, &$form_state, $form_id) {
  // Loop through the array of module packages.
  foreach (element_children($form['modules']) as $package) {
    // Loop through the array of module rows for each package.
    foreach (element_children($form['modules'][$package]) as $module_filename) {
      // Add an extra operation link for each module row.
      $form['modules'][$package][$module_filename]['links']['google'] = array(
        '#type' => 'link',
        '#title' => t('Google'),
        '#href' => 'http://google.com',
        '#options' => array(
          'attributes' => array(
            'class' => array('module-link', 'module-link-google'),
            'title' => t('Go to google'),
          ),
        ),
      );
    }
  }
}

You can also use relative internal links instead of the external google link above.

  • Thanks a lot. :) I will try this. I also want to know a function which returns list of contrib modules only. module_list also returns core modules. – user46121 Apr 9 '15 at 13:54
  • This function returns an error stating "Cannot use string offset as an array". I will look into it. – user46121 Apr 9 '15 at 13:57
  • I am still trying to figure it out. Do you know why this happens. In the array returned by $form['modules'] the first element is '#tree' . May be this might be the reason – user46121 Apr 9 '15 at 14:21
  • Oops sorry, I typed the code straight into my answer without actually testing it. I made a change that should fix it but I still haven't tested it. I will have a minute to properly test it shortly. – rooby Apr 9 '15 at 15:12
  • Thanks again. Now the error message has gone but still the link doesn't appear. – user46121 Apr 9 '15 at 15:35

Your Answer

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