<?php

/**
 * @file
 * Contains nodehive_content_browser.module.
 */

use Drupal\Core\Url;

/**
 * Implements hook_toolbar().
 */
function nodehive_content_browser_toolbar() {
  $items = [];

  // Check if user has permission to use content finder.
  if (\Drupal::currentUser()->hasPermission('use content finder')) {
    $items['content_finder'] = [
      '#type' => 'toolbar_item',
      '#weight' => 150,
      'tab' => [
        '#type' => 'link',
        '#title' => t('Content Finder'),
        '#url' => Url::fromUserInput('#content-finder'),
        '#attributes' => [
          'title' => t('Find content quickly (Cmd+Shift+F / Ctrl+Shift+F)'),
          'class' => ['toolbar-icon', 'toolbar-icon-content-finder'],
          'id' => 'toolbar-item-content-finder',
          'data-dialog-type' => 'content-finder',
        ],
      ],
      '#attached' => [
        'library' => ['nodehive_content_browser/content-finder'],
        'drupalSettings' => [
          'contentFinder' => [
            'searchUrl' => Url::fromRoute('nodehive_content_browser.search')->toString(),
            'contentTypesUrl' => Url::fromRoute('nodehive_content_browser.content_types')->toString(),
          ],
        ],
      ],
    ];
  }

  return $items;
}

/**
 * Implements hook_theme().
 */
function nodehive_content_browser_theme() {
  return [
    'content_finder_overlay' => [
      'variables' => [],
      'template' => 'content-finder-overlay',
    ],
  ];
}
