You are here

function openlayers_contextual_links_openlayers_object_postprocess_alter in Openlayers 7.3

Implements hook_openlayers_object_postprocess_alter().

File

modules/openlayers_contextual_links/openlayers_contextual_links.module, line 10
Openlayers Contextual Links module.

Code

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';
}