You are here

ajax_links_api.module in Ajaxify Drupal with JQuery Ajax 8

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

Make any links or create new links via jQuery Ajax.

File

ajax_links_api.module
View source
<?php

/**
 * @file
 * Make any links or create new links via jQuery Ajax.
 */
use Drupal\Core\Url;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\ajax_links_api\Form\AjaxLinksApiSettingsForm;

/**
 * Implements hook_page_attachments().
 *
 * Insert JavaScript to the appropriate scope/region of the page.
 */
function ajax_links_api_page_attachments(array &$page) {
  $config = \Drupal::config('ajax_links_api.admin_settings');

  // Get the positive/negative triggers for ajax links.
  list($trigger, $negative_trigger) = AjaxLinksApiSettingsForm::ajaxLinksApiGetTriggers();
  $page['#attached']['drupalSettings']['data']['ajax_links_api'] = array(
    'html5' => $config
      ->get('ajax_links_api.html5'),
    'vpager' => $config
      ->get('ajax_links_api.vpager'),
    'trigger' => $trigger,
    'selector' => $config
      ->get('ajax_links_api.selector'),
    'negative_triggers' => $negative_trigger,
    'scripts_included' => $config
      ->get('ajax_links_api.scripts_included'),
  );
  $page['#attached']['library'][] = 'ajax_links_api/ajax_links_api';
}

/**
 * Implements hook_help().
 */
function ajax_links_api_help($route_name, RouteMatchInterface $route_match) {
  $ajax_links_api_settings_url = Url::fromRoute('ajax_links_api.admin_settings');
  $ajax_links_api_demo_url = Url::fromRoute('ajax_links_api.demo');
  $output = '<h2><strong>How to use Ajax links API</strong></h2>';
  $output .= t('<p><strong>Method 1</strong> : You can ajaxify any links by specifying the Class/Id in @admin.', array(
    '@admin' => \Drupal::l(t('Module settings page'), $ajax_links_api_settings_url),
  ));
  $output .= ' Target DIV will be default Target DIV defined in module config page.</p>';
  $output .= '<p>' . t('Example : <code>@example1</code> . You can ajaxify this link by adding', array(
    '@example1' => '<a class="test" href="node/add/page">Add page</a>',
  ));
  $output .= '<code>.test</code> in module config page.</p>';
  $output .= '<p><strong>Method 2</strong> : Use this in your module,</p>';
  $output .= '<p><code>$service = \\Drupal::service("ajax_links_api.ajax_link");<br>';
  $output .= '$link = $service->lAjax($title, $path, $target, $link_options);</code></p>';
  $output .= '<p>* <code>$title</code>: Title to display.<br />';
  $output .= '* <code>$path</code> : Drupal path. <br />';
  $output .= '* <code>$target (optional)</code>: ID or CLASS of DIV to be replaced. You can override default Target DIV defined by specifying <code>$target</code>.</p>';
  $output .= '<p>Example : </p>';
  $output .= '<p><code>$service = \\Drupal::service("ajax_links_api.ajax_link");<br>';
  $output .= '$link = $service->lAjax("add page", "/node/add/page", "#content", array("#attributes" => "class" => array("ajax-links-api")));</code></p>';
  $output .= '<p><strong>Method 3</strong> : Add <code>class="ajax-link"</code> to any link. Target div will be Target DIV defined . You can change default Target DIV or override target by specifying attribute <code>rel=""</code>.</p>';
  $output .= '<p>' . t('Example : <code>@example2</code></p>', array(
    '@example2' => '<a class="ajax-link" href="node/add/page" rel="#content">Add page</a>',
  ));
  $output .= t('<h2>@demo</h2>', array(
    '@demo' => \Drupal::l(t('DEMO'), $ajax_links_api_demo_url),
  ));
  switch ($route_name) {
    case 'ajax_links_api.admin_settings':
    case 'help.page.ajax_links_api':
    case 'ajax_links_api.demo':
      return $output;
  }
}

/**
 * Implements hook_theme().
 */
function ajax_links_api_theme() {
  return array(
    'page__ajax' => array(
      'render element' => 'elements',
      'base hook' => 'page',
    ),
    'html__ajax' => array(
      'render element' => 'html',
      'base hook' => 'html',
    ),
  );
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function ajax_links_api_theme_suggestions_page(array $variables) {
  $template_suggestions = array();
  $suggestions = array();

  // This is used to display only $content.
  if (isset($_GET['ajax']) && $_GET['ajax'] == 1) {
    $template_suggestions[] = 'page';

    // To override page--ajax.tpl.php for content types.
    if (isset($variables['node'])) {
      $template_suggestions[] = 'page__' . $variables['node']->type;
    }

    // To override page--ajax.tpl.php for individual path.
    if (\Drupal::service('path.matcher')
      ->isFrontPage()) {
      $path_args = [
        '',
      ];
    }
    else {
      $path_args = explode('/', Url::fromRoute('<current>')
        ->getInternalPath());
    }
    if ($suggestions = theme_get_suggestions($path_args, 'page')) {
      foreach ($template_suggestions as $suggestion) {
        $suggestions[] = $suggestion . '__ajax';
      }
    }
  }
  return $suggestions;
}

/**
 * Implements hook_theme_suggestions_HOOK().
 */
function ajax_links_api_theme_suggestions_html(array $variables) {
  $template_suggestions = array();
  $suggestions = array();

  // This is used to display only $content.
  if (isset($_GET['ajax']) && $_GET['ajax'] == 1) {
    $template_suggestions[] = 'html';

    // To override html--ajax.tpl.php for content types.
    if (isset($variables['node'])) {
      $template_suggestions[] = 'html__' . $variables['node']->type;
    }

    // To override html--ajax.tpl.php for individual path.
    if (\Drupal::service('path.matcher')
      ->isFrontPage()) {
      $path_args = [
        '',
      ];
    }
    else {
      $path_args = explode('/', Url::fromRoute('<current>')
        ->getInternalPath());
    }
    if ($suggestions = theme_get_suggestions($path_args, 'html')) {
      foreach ($template_suggestions as $suggestion) {
        $suggestions[] = $suggestion . '__ajax';
      }
    }
  }
  return $suggestions;
}

/**
 * Implements hook_theme_registry_alter().
 */
function ajax_links_api_theme_registry_alter(&$theme_registry) {
  $mod_path = drupal_get_path('module', 'ajax_links_api') . '/tpl';

  // Munge on a copy.
  $theme_registry_copy = $theme_registry;
  $theme_registry += array_diff_key($theme_registry_copy, $theme_registry);
  $hooks = array(
    'page',
  );
  foreach ($hooks as $h) {
    if (is_array($theme_registry[$h]['theme path'])) {
      $first_element = array_shift($theme_registry[$h]['theme path']);
      array_unshift($theme_registry[$h]['theme path'], $first_element, $mod_path);
    }
  }
}