cforge-2.0.x-dev/modules/cforge_gallery/cforge_gallery.module

modules/cforge_gallery/cforge_gallery.module
<?php

/**
 * @file
 * Hooks for cforge_gallery module.
 */

use Drupal\Core\Url;
use Drupal\node\Entity\Node;

/**
 * Implements hook_theme().
 */
function cforge_gallery_theme() {
  return [
    'gallery_preview' => [
      'variables' => [
        'taxonomy_term' => 0,
        'nids' => [],
      ]
    ]
  ];
}

/**
 * Default_implementation of theme hook 'gallery_preview'.
 */
function template_preprocess_gallery_preview(&$vars) {
  $gallery = $vars['taxonomy_term'];
  $nid = $vars['nids'][rand(0, count($vars['nids']) - 1)];
  $image = cforge_gallery_format_photo($nid);
  $vars['image'] = \Drupal::service('renderer')->render($image);
  $vars['title'] = $gallery->label();
  $vars['description'] = $gallery->getDescription();
  $vars['url'] = Url::fromRoute(
    'view.gallery.page_1',
    ['arg_0' => $gallery->id()],
    ['title' => t("View '@name' gallery", ['@name' => $gallery->label()])]
  )->toString();
}

/**
 * Format a photo for the gallery.
 *
 * @param int $nid
 *   The node id of the photo.
 *
 * @return array
 *   A renderable array.
 */
function cforge_gallery_format_photo($nid) : array {
  $node = Node::load($nid);
  if (!$node->terms->getValue()) {
    \Drupal::messenger()->addError("Gallery term for photo node $nid term was not imported");
    return [];
  }
  return [
    '#theme' => 'image_formatter',
    '#item' => $node->image->first(),
    '#image_style' => 'medium'
  ];
}

/**
 * Implements hook_form_FORM_ID_alter().
 */
function cforge_gallery_form_node_form_alter(&$form, $form_state) {
  $node = $form_state->getFormObject()->getEntity();
  if ($node->bundle() == 'image') {
    $form['path']['#access'] = FALSE;
  }
}


/**
 * Implements hook_migration_plugins_alter().
 */
function cforge_gallery_migration_plugins_alter(array &$definitions)  {
  $definitions['d7_block']['process']['plugin'][0]['map']['cforge_gallery']['random_photos'] = 'cforge_random_photo';
}

/**
 * Implements hook_preprocess_image_formatter().
 */
function cforge_gallery_preprocess_image_formatter(&$vars) {
  if ($vars['image_style'] <> 'thumbnail') {
    $vars['caption'] = $vars['image']['#alt'];
  }
}

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

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