You are here

openlayers_contextual_links.module in Openlayers 7.3

Openlayers Contextual Links module.

File

modules/openlayers_contextual_links/openlayers_contextual_links.module
View source
<?php

/**
 * @file
 * Openlayers Contextual Links module.
 */

/**
 * Implements hook_openlayers_object_postprocess_alter().
 */
function openlayers_contextual_links_openlayers_object_postprocess_alter(array &$build, \Drupal\openlayers\Types\ObjectInterface $context) {
  if (!user_access('administer openlayers')) {
    return FALSE;
  }
  if (!$context instanceof Drupal\openlayers\Types\MapInterface) {
    return FALSE;
  }
  if ($context
    ->getOption('contextualLinks', FALSE) == FALSE) {
    unset($build['contextual_links']);
    return FALSE;
  }
  $current_path = current_path();
  if ('system/ajax' == $current_path && isset($_SESSION['current_path'])) {
    $current_path = $_SESSION['current_path'];
  }
  $links = array(
    'openlayers' => array(
      'title' => 'Edit this map',
      'href' => 'admin/structure/openlayers/maps/list/' . $context
        ->getMachineName() . '/edit',
      'query' => array(
        'destination' => $current_path,
      ),
    ),
  );
  $objects = $context
    ->getCollection()
    ->getFlatList(\Drupal\openlayers\Openlayers::getPluginTypes(array(
    'map',
  )));
  ksort($objects);
  foreach ($objects as $object) {
    $object_links = array();

    // Build contextual link for this object.
    $name = $object
      ->getName();
    if (empty($name)) {
      $name = $object
        ->getMachineName();
    }
    $object_links[$object
      ->getType() . ':' . $object
      ->getMachineName()] = array(
      'title' => t('Edit @object_name', array(
        '@object_name' => $name,
      )),
      'href' => 'admin/structure/openlayers/' . $object
        ->getType() . 's/list/' . $object
        ->getMachineName() . '/edit',
      'query' => array(
        'destination' => $current_path,
      ),
    );
    if (!empty($object_links)) {

      // Build contextual link title for this type.
      $links[$object
        ->getType()] = array(
        'title' => '<strong>' . ucwords($object
          ->getType() . 's') . '</strong>',
        'html' => TRUE,
      );
      $links += $object_links;
    }
  }
  $build['map_prefix']['contextual_links'] = array(
    '#prefix' => '<div class="contextual-links-wrapper">',
    '#suffix' => '</div>',
    '#theme' => 'links__contextual',
    '#links' => $links,
    '#attributes' => array(
      'class' => array(
        'contextual-links',
      ),
    ),
    '#attached' => array(
      'library' => array(
        array(
          'contextual',
          'contextual-links',
        ),
      ),
    ),
  );
  $build['openlayers-container']['#attributes']['class'][] = 'contextual-links-region';
}

/**
 * Implements hook_form_BASE_FORM_ID_alter().
 */
function openlayers_contextual_links_form_openlayers_map_form_settings_alter(&$form, &$form_state) {
  if (($map = \Drupal\openlayers\Openlayers::load('Map', $form_state['item'])) == TRUE) {
    $form['options']['ui']['contextualLinks'] = array(
      '#type' => 'checkbox',
      '#title' => 'Contextual links',
      '#description' => t('Enable contextual links on the map.'),
      '#default_value' => $map
        ->getOption('contextualLinks', FALSE),
      '#parents' => array(
        'options',
        'contextualLinks',
      ),
    );
  }
}