Skip to content
outside_in.module 5.95 KiB
Newer Older
<?php

/**
 * @file
 * Allows configuring blocks and other configuration from the site front-end.
 */

use Drupal\Core\Asset\AttachedAssetsInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\outside_in\Block\BlockEntityOffCanvasForm;
use Drupal\outside_in\Form\SystemBrandingOffCanvasForm;
use Drupal\outside_in\Form\SystemMenuOffCanvasForm;

/**
 * Implements hook_help().
 */
function outside_in_help($route_name, RouteMatchInterface $route_match) {
  switch ($route_name) {
    case 'help.page.outside_in':
      $output = '<h3>' . t('About') . '</h3>';
      $output .= '<p>' . t('The Settings Tray module provides an \'edit mode\' in which clicking on a block opens a slide-out tray which allows configuration to be altered without leaving the page.For more information, see the <a href=":outside-in-documentation">online documentation for the Settings Tray module</a>.', [':outside-in-documentation' => 'https://www.drupal.org/documentation/modules/outside_in']) . '</p>';
      $output .= '<h3>' . t('Uses') . '</h3>';
      $output .= '<dl>';
      $output .= '<dt>' . t('Editing blocks on the same page in the slide-out tray') . '</dt>';
      $output .= '</dl>';
      return ['#markup' => $output];
  }
}

/**
 * Implements hook_contextual_links_view_alter().
 *
 * Change Configure Blocks into offcanvas links.
 */
function outside_in_contextual_links_view_alter(&$element, $items) {
  if (isset($element['#links']['outside-inblock-configure'])) {
    // Place outside_in link first.
    $outside_in_link = $element['#links']['outside-inblock-configure'];
    unset($element['#links']['outside-inblock-configure']);
    $element['#links'] = ['outside-inblock-configure' => $outside_in_link] + $element['#links'];

    $element['#links']['outside-inblock-configure']['attributes'] = [
      'class' => ['use-ajax'],
      'data-dialog-type' => 'dialog',
      'data-dialog-renderer' => 'offcanvas',
    // If this is content block change title to avoid duplicate "Quick Edit".
    if (isset($element['#links']['block-contentblock-edit'])) {
      $element['#links']['outside-inblock-configure']['title'] = t('Quick edit settings');
    }

    $element['#attached']['library'][] = 'outside_in/drupal.off_canvas';
  }
}

/**
 * Implements hook_block_view_alter().
 */
function outside_in_block_view_alter(array &$build) {
  // Force a new 'data-contextual-id' attribute on blocks when this module is
  // enabled so as not to reuse stale data cached client-side.
  // @todo Remove when https://www.drupal.org/node/2773591 is fixed.
  $build['#contextual_links']['outside_in'] = [
    'route_parameters' => [],
  ];
}

/**
 * Implements hook_element_info_alter().
function outside_in_element_info_alter(&$type) {
  if (isset($type['page'])) {
    $type['page']['#theme_wrappers']['outside_in_page_wrapper'] = ['#weight' => -1000];
  }
 * Implements hook_theme().
function outside_in_theme() {
  return [
    'outside_in_page_wrapper' => [
      'variables' => ['children' => NULL],
    ],
  ];
}

/**
 * Implements hook_entity_type_build().
 */
function outside_in_entity_type_build(array &$entity_types) {
  /* @var $entity_types \Drupal\Core\Entity\EntityTypeInterface[] */
  $entity_types['block']
    ->setFormClass('offcanvas', BlockEntityOffCanvasForm::class)
    ->setLinkTemplate('offcanvas-form', '/admin/structure/block/manage/{block}/offcanvas');
}

/**
 * Implements hook_preprocess_HOOK() for block templates.
 */
function outside_in_preprocess_block(&$variables) {
  // The main system block does not contain the block contextual links.
  $variables['#cache']['contexts'][] = 'outside_in_is_applied';
  if ($variables['plugin_id'] !== 'system_main_block' && \Drupal::service('outside_in.manager')->isApplicable()) {
    // Add class and attributes to all blocks to allow Javascript to target.
    $variables['attributes']['class'][] = 'outside-in-editable';
    $variables['attributes']['data-drupal-outsidein'] = 'editable';
  }
}

/**
 * Implements hook_toolbar_alter().
 *
 * Includes outside_library if Edit link is in toolbar.
 */
function outside_in_toolbar_alter(&$items) {
  $items['contextual']['#cache']['contexts'][] = 'outside_in_is_applied';
  if (isset($items['contextual']['tab']) && \Drupal::service('outside_in.manager')->isApplicable()) {
    $items['contextual']['#weight'] = -1000;
    $items['contextual']['#attached']['library'][] = 'outside_in/drupal.outside_in';
    $items['contextual']['tab']['#attributes']['data-drupal-outsidein'] = 'toggle';

    // Set a class on items to mark whether they should be active in edit mode.
    // @todo Create a dynamic method for modules to set their own items.
    //   https://www.drupal.org/node/2784589

    $edit_mode_items = ['contextual', 'block_place'];
    foreach ($items as $key => $item) {
      if (!in_array($key, $edit_mode_items) && (!isset($items[$key]['#wrapper_attributes']['class']) || !in_array('hidden', $items[$key]['#wrapper_attributes']['class']))) {
        $items[$key]['#wrapper_attributes']['class'][] = 'edit-mode-inactive';
      }
    }
  }
}

/**
 * Implements hook_block_alter().
 */
function outside_in_block_alter(&$definitions) {
  if (!empty($definitions['system_branding_block'])) {
    $definitions['system_branding_block']['forms']['offcanvas'] = SystemBrandingOffCanvasForm::class;
  }

  // Since menu blocks use derivatives, check the definition ID instead of
  // relying on the plugin ID.
  foreach ($definitions as &$definition) {
    if ($definition['id'] === 'system_menu_block') {
      $definition['forms']['offcanvas'] = SystemMenuOffCanvasForm::class;
    }
  }
}

/**
 * Implements hook_css_alter().
 */
function outside_in_css_alter(&$css, AttachedAssetsInterface $assets) {
  // @todo Remove once conditional ordering is introduced in
  //   https://www.drupal.org/node/1945262.
  $path = drupal_get_path('module', 'outside_in') . '/css/outside_in.theme.css';
  if (isset($css[$path])) {
    // Use 200 to come after CSS_AGGREGATE_THEME.
    $css[$path]['group'] = 200;
  }
}