You are here

farm_ui_theme.module in farmOS 2.x

The farmOS UI Theme module.

File

modules/core/ui/theme/farm_ui_theme.module
View source
<?php

/**
 * @file
 * The farmOS UI Theme module.
 */
use Drupal\Core\Block\BlockPluginInterface;

/**
 * Implements hook_element_info_alter().
 */
function farm_ui_theme_element_info_alter(array &$info) {
  if (isset($info['farm_map'])) {
    $info['farm_map']['#attached']['library'][] = 'farm_ui_theme/map';
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function farm_ui_theme_preprocess_block(&$variables) {
  if ($variables['plugin_id'] == 'help_block') {
    $variables['#attached']['library'][] = 'farm_ui_theme/help';
  }
}

/**
 * Implements hook_preprocess_HOOK().
 */
function farm_ui_theme_preprocess_farm_map(&$variables) {

  // We need to add the flag CSS to pages with maps in them because a user MAY
  // click a popup which MAY have flags in it. Otherwise the flags do not get
  // styled.
  $variables['#attached']['library'] = 'farm_ui_theme/flag';
}

/**
 * Implements hook_preprocess_HOOK().
 */
function farm_ui_theme_preprocess_toolbar(&$variables) {
  $variables['#attached']['library'][] = 'farm_ui_theme/toolbar';
}

/**
 * Implements hook_block_view_BASE_BLOCK_ID_alter().
 */
function farm_ui_theme_block_view_farm_powered_by_block_alter(array &$build, BlockPluginInterface $block) {
  $build['#attached']['library'][] = 'farm_ui_theme/footer';
}

/**
 * Implements hook_preprocess_HOOK().
 */
function farm_ui_theme_preprocess_field__flag(array &$variables) {
  $variables['#attached']['library'][] = 'farm_ui_theme/flag';
}

/**
 * Implements hook_preprocess_HOOK().
 */
function farm_ui_theme_preprocess_asset__full(&$variables) {
  farm_ui_theme_build_stacked_twocol_layout($variables, 'asset');
  $variables['#attached']['library'][] = 'farm_ui_theme/layout';
}

/**
 * Implements hook_preprocess_HOOK().
 */
function farm_ui_theme_preprocess_log__full(&$variables) {
  farm_ui_theme_build_stacked_twocol_layout($variables, 'log');
  $variables['#attached']['library'][] = 'farm_ui_theme/layout';
}

/**
 * Implements hook_preprocess_HOOK().
 */
function farm_ui_theme_preprocess_plan__full(&$variables) {
  farm_ui_theme_build_stacked_twocol_layout($variables, 'plan');
  $variables['#attached']['library'][] = 'farm_ui_theme/layout';
}

/**
 * Splits content into a stacked two-column layout.
 *
 * @param array $variables
 *   A $variables array that contains a 'content' item, which will be replaced
 *   by a stacked two-column layout.
 * @param string $entity_type
 *   The entity type.
 */
function farm_ui_theme_build_stacked_twocol_layout(array &$variables, string $entity_type) {

  // Ask modules for a list of region items.
  $region_items = \Drupal::moduleHandler()
    ->invokeAll('farm_ui_theme_region_items', [
    $entity_type,
  ]);

  // Split the content items into regions.
  $regions = [];
  foreach ($variables['content'] as $index => $item) {
    foreach ($region_items as $region_name => $items) {
      $region = 'first';
      if (in_array($index, $items)) {
        $region = $region_name;
        break;
      }
    }
    $regions[$region][$index] = $item;
  }

  // Build the layout.

  /** @var \Drupal\Core\Layout\LayoutInterface $layoutInstance */
  $layout = \Drupal::service('plugin.manager.core.layout')
    ->createInstance('layout_twocol', []);
  $variables['content'] = $layout
    ->build($regions);
}

/**
 * Implements hook_farm_ui_theme_region_items().
 */
function farm_ui_theme_farm_ui_theme_region_items(string $entity_type) {

  // Define common asset, log, and plan region items on behalf of core modules.
  switch ($entity_type) {
    case 'asset':
      return [
        'top' => [
          'geometry',
        ],
        'first' => [],
        'second' => [
          'image',
          'inventory',
          'is_location',
          'is_fixed',
          'location',
          'status',
          'type',
        ],
        'bottom' => [
          'api',
          'file',
        ],
      ];
    case 'log':
      return [
        'top' => [
          'geometry',
        ],
        'first' => [],
        'second' => [
          'image',
          'status',
          'type',
        ],
        'bottom' => [
          'file',
        ],
      ];
    case 'plan':
      return [
        'top' => [],
        'first' => [],
        'second' => [
          'image',
          'status',
          'type',
        ],
        'bottom' => [
          'file',
        ],
      ];
    default:
      return [];
  }
}

/**
 * Implements hook_farm_update_exclude_config().
 */
function farm_ui_theme_farm_update_exclude_config() {

  // Exclude config that we have overridden in hook_install().
  return [
    'block.block.gin_local_actions',
  ];
}