You are here

mpac.module in Multi-path autocomplete 8

Same filename and directory in other branches
  1. 6 mpac.module
  2. 7 mpac.module

Find node paths on menu item creation via autocomplete.

File

mpac.module
View source
<?php

/**
 * @file
 * Find node paths on menu item creation via autocomplete.
 */

/**
 * Implements hook_menu().
 */
function mpac_menu() {
  $items['admin/config/search/mpac'] = array(
    'title' => 'Multi-path autocomplete',
    'description' => 'Change settings for Multi-path autocomplete.',
    'route_name' => 'mpac.autocomplete_settings',
  );
  return $items;
}

/**
 * Gets the selection handlers for a given mpac autocomplete type.
 *
 * @param string $type
 *   Type of handlers to load (i.e. "path").
 *
 * @return array
 *   List of \Drupal\mpac\Plugin\Type\Selection\SelectionInterface.
 */
function mpac_get_selection_handlers($type) {
  $handlers = array();
  $plugin_manager = \Drupal::service('plugin.manager.mpac.selection');

  // Load all available plugins.
  $plugins = $plugin_manager
    ->getDefinitions();

  // Sort the selection plugins by weight.
  uasort($plugins, array(
    'Drupal\\Component\\Utility\\SortArray',
    'sortByWeightElement',
  ));
  foreach ($plugins as $key => $plugin_info) {
    if (!empty($plugin_info['types']) && count(array_intersect(array(
      $type,
      '*',
    ), $plugin_info['types'])) > 0) {
      $plugin = $plugin_manager
        ->createInstance($plugin_info['id']);
      $handlers[$key] = $plugin;
    }
  }

  // Allow other modules to alter the list of handlers for a type.
  \Drupal::moduleHandler()
    ->alter('mpac_selection_handlers', $handlers, $type);
  return $handlers;
}

/**
 * Collect information about selection plugins provided by other modules.
 */
function mpac_selection_plugin_info() {
  return Drupal::moduleHandler()
    ->invokeAll('mpac_selection_plugin_info');
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mpac_form_menu_link_form_alter(&$form, $form_state) {
  if (empty($form['link_path']['#autocomplete_path'])) {
    $form['link_path']['#autocomplete_route_name'] = 'mpac.autocomplete';
    $form['link_path']['#autocomplete_route_parameters'] = array(
      'type' => 'path',
    );
    $form['link_path']['#description'] .= '<br />' . t('To get a list of items allowed here you may simply enter the title of previously created content or a part of the path to the content.');
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function mpac_form_path_admin_form_alter(&$form, $form_state) {
  if (empty($form['source']['#autocomplete_path'])) {
    $form['source']['#autocomplete_route_name'] = 'mpac.autocomplete';
    $form['source']['#autocomplete_route_parameters'] = array(
      'type' => 'path',
    );
    $form['source']['#description'] .= '<br />' . t('To get a list of items allowed here you may simply enter the title of previously created content or a part of the path to the content.');
  }
}

Functions

Namesort descending Description
mpac_form_menu_link_form_alter Implements hook_form_FORM_ID_alter().
mpac_form_path_admin_form_alter Implements hook_form_FORM_ID_alter().
mpac_get_selection_handlers Gets the selection handlers for a given mpac autocomplete type.
mpac_menu Implements hook_menu().
mpac_selection_plugin_info Collect information about selection plugins provided by other modules.