io-8.x-1.x-dev/io.module

io.module
<?php

/**
 * @file
 * Provides integration with Intersection Observer API.
 */

use Drupal\Core\Block\BlockPluginInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Template\Attribute;
use Drupal\block\BlockInterface;
use Drupal\block\Entity\Block;
use Drupal\io\IoManager;

/**
 * Provides a convenient shortcut for procedural hooks.
 *
 * @return \Drupal\io\IoManager
 *   The Intersection Observer manager class instance.
 */
// @codingStandardsIgnoreStart
function io(): IoManager {
  static $manager;

  if (!isset($manager)) {
    $manager = \Drupal::service('io.manager');
  }

  return $manager;
}
// @codingStandardsIgnoreEnd

/**
 * Implements hook_preprocess_HOOK().
 */
function io_preprocess_block(&$variables) {
  if (isset($variables['elements']['#io'])) {
    io()->preprocessBlock($variables);
  }
}

/**
 * Implements hook_block_view_alter().
 */
function io_block_view_alter(array &$build, BlockPluginInterface $block_plugin) {
  if (io()->isIoBlockDisabled()) {
    return;
  }

  // Adds IO identifier for template_preprocess_block() fast check. Fast as we
  // don't (re-)load the block entity at runtime just to get the UUID.
  // Not all block has the getThirdPartySetting, somebody must deviate.
  /** @var \Drupal\block\Entity\Block $block */
  $block = $build['#block'] ?? NULL;
  if ($block instanceof Block) {
    if (method_exists($block, 'getThirdPartySetting')
      && $block->getThirdPartySetting('io', 'lazyload')) {
      $build['#io'] = $block->uuid();
      $build['#attached']['drupalSettings']['io']['block'] = io()->getIoSettings('block');
      $build['#attached']['library'][] = 'io/block';
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function io_form_block_form_alter(&$form, FormStateInterface $form_state, $form_id) {
  if (io()->currentUser()->hasPermission('administer blocks')) {
    io()->blockFormAlter($form, $form_state, $form_id);
  }
}

/**
 * Implements hook_ENTITY_TYPE_presave().
 */
function io_block_presave(BlockInterface $block) {
  $disabled = !io()->isBlockApplicable($block);
  if (method_exists($block, 'getThirdPartySetting')) {
    if (empty($block->getThirdPartySetting('io', 'lazyload')) || $disabled) {
      if ($disabled) {
        $block->setThirdPartySetting('io', 'lazyload', FALSE);
      }
      $block->unsetThirdPartySetting('io', 'lazyload');
    }
  }
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function io_form_blazy_settings_form_alter(array &$form, $form_state) {
  io()->blazySettingsFormAlter($form);
}

/**
 * Implements hook_config_schema_info_alter().
 */
function io_config_schema_info_alter(array &$definitions) {
  if (isset($definitions['blazy.settings'])) {
    $mappings = &$definitions['blazy.settings']['mapping']['extras']['mapping'];
    $mappings['io_fallback']['type'] = 'string';
    $mappings['io_block_disabled']['type'] = 'boolean';
  }
}

/**
 * Implements hook_preprocess_io_pager().
 */
function io_preprocess_io_pager(&$variables) {
  io()->preprocessIoPager($variables);
}

/**
 * Overrides template_preprocess_views_view().
 */
function io_preprocess_views_view(&$variables) {
  if (io()->getIoPager($variables['view'])) {
    $plugin_id = $variables['view']->getStyle()->getPluginId();
    if (!empty($variables['rows'])) {
      if (!in_array($plugin_id, ['html_list', 'table'])) {
        if (!isset($variables['rows']['#theme_wrappers'])) {
          $variables['rows']['#theme_wrappers'][] = 'container';
        }
        $variables['rows']['#attributes']['data-io-pager'] = TRUE;
      }
    }

    // Ensures the library is loaded regardless of rows empty, or not.
    $variables['attributes']['data-io-view'] = $plugin_id;
    $variables['#attached']['library'][] = 'io/pager';
    $variables['#attached']['drupalSettings']['io']['pager'] = io()->getIoSettings('pager');
  }
}

/**
 * Overrides template_preprocess_views_view_list().
 */
function io_preprocess_views_view_list(&$variables) {
  if (io()->getIoPager($variables['view']) && !empty($variables['rows'])) {
    $variables['list']['attributes'] = empty($variables['list']['attributes'])
      ? new Attribute() : $variables['list']['attributes'];
    $variables['list']['attributes']['data-io-pager'] = TRUE;
  }
}

/**
 * Implements hook_help().
 */
function io_help($route_name) {
  if ($route_name == 'help.page.io') {
    $output = file_get_contents(dirname(__FILE__) . '/README.md');
    return blazy()->markdown($output);
  }
  return '';
}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc