You are here

content_browser.module in Content Browser 8

Contains logic for content_browser.

File

content_browser.module
View source
<?php

/**
 * @file
 * Contains logic for content_browser.
 */
use Drupal\views\ViewExecutable;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Form\FormStateInterface;

/**
 * Implements hook_help().
 */
function content_browser_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {

    // Main module help for the content_browser module.
    case 'help.page.content_browser':
      $output = '';
      $output .= '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('Provides an Entity Browser for Content Entities.') . '</p>';
      return $output;
    default:
  }
}

/**
 * Implements hook_form_alter().
 */
function content_browser_form_alter(&$form, FormStateInterface &$form_state) {
  if (isset($form['#form_id'])) {
    if ($form['#form_id'] == 'entity_browser_browse_content_form' || $form['#form_id'] == 'entity_browser_browse_content_iframe_form') {

      // Attach our library.
      $form['#attached']['library'][] = 'content_browser/view';

      // Wrap actions so we can float them at the bottom of the browser.
      $form['actions_wrap'] = [
        '#type' => 'container',
        '#attributes' => [
          'class' => [
            'content-browser-actions',
          ],
        ],
        0 => $form['widget']['actions'],
      ];
      unset($form['widget']['actions']);

      // Add a class for generic styling.
      $form['#attributes']['class'][] = 'content-browser-form';
    }
    elseif (isset($form['#id']) && $form['#id'] == 'views-exposed-form-content-browser-entity-browser') {

      /** @var \Drupal\views\ViewExecutable $view */
      $view = $form_state
        ->get('view');
      if ($view instanceof ViewExecutable && isset($view->argument['type'])) {

        /** @var \Drupal\node\Plugin\views\argument\Type $type */
        $type = $view->argument['type'];
        $value = $type
          ->getValue();
        if (!is_null($value) && !$type
          ->isException($value)) {
          $types = explode('+', $value);
          $types[] = 'All';
          foreach ($form['type']['#options'] as $name => $option) {
            if (!in_array($name, $types, TRUE)) {
              unset($form['type']['#options'][$name]);
            }
          }
        }
      }
    }
  }
}

/**
 * Implements hook_library_info_alter().
 */
function content_browser_library_info_alter(&$libraries, $extension) {

  // Optionally use the Libraries module to determine our library paths.
  if ($extension == 'content_browser' && \Drupal::moduleHandler()
    ->moduleExists('libraries')) {
    $masonry_path = libraries_get_path('masonry') . '/dist/masonry.pkgd.min.js';
    $libraries['masonry']['js'] = [
      '/' . $masonry_path => [
        'minified' => 'true',
      ],
    ];
  }
}