You are here

views_isotope_example.module in Brainstorm profile 7

Simple example module.

File

modules/custom/views_isotope/views_isotope_example/views_isotope_example.module
View source
<?php

/**
 * @file
 * Simple example module.
 */

/**
 * Page callback.
 *
 * This is an example of using standard theme functions to output your own
 * lists, without using views.
 *
 * @return array
 *   Drupal render array.
 */
function views_isotope_example_theme_page() {
  $return[] = [
    '#markup' => t('<p>You can explore this example for yourself at <strong>views_isotope_example_page()</strong>.</p>'),
  ];

  // The label can be anything, including images, etc. Filtering happens on the
  // key.
  $colors = [
    'blue' => 'Blue',
    'red' => '<span class="thisIsCustomisedLabel">Red</span>',
    'yellow' => 'Yellow',
  ];
  $return[] = [
    '#theme' => 'isotope_filter',
    '#items' => $colors,
    // Optional filtername: unique to distinguish it from the other filters on
    // the page.
    '#filtername' => 'color',
    // Optional filtertitle: displayed as a list title.
    '#filtertitle' => t('Colour'),
  ];
  $sizes = [
    'small' => 'Small',
    'wide' => 'Wide',
    'big' => 'Big',
    'tall' => 'Tall',
  ];
  $return[] = [
    '#theme' => 'isotope_filter',
    '#items' => $sizes,
    '#filtername' => 'size',
    '#filtertitle' => t('Size'),
  ];
  $shapes = [
    'round' => 'Round',
    'square' => 'Square',
  ];
  $return[] = [
    '#theme' => 'isotope_filter',
    '#items' => $shapes,
    '#filtername' => 'shape',
    '#filtertitle' => t('Shapes'),
    '#instance' => 1,
  ];
  $sorts = [
    'Size' => 'size',
    'Shape' => 'shape',
    'Color' => 'color',
    'Color then Size' => [
      'color',
      'size',
    ],
    'Color then Shape' => [
      'color',
      'shape',
    ],
  ];
  $return[] = [
    '#theme' => 'isotope_sorter',
    '#sorts' => $sorts,
    '#original' => 'Original',
    '#instance' => 1,
  ];

  // Create items of every size shape and color.
  $items = [];
  foreach ($sizes as $size => $label1) {
    foreach ($shapes as $shape => $label2) {
      foreach ($colors as $color => $label3) {
        $items[] = [
          'value' => '<p>Item</p>',
          'data' => [
            'size' => $size,
            'shape' => $shape,
            'color' => $color,
          ],
        ];
      }
    }
  }
  $return[] = [
    '#theme' => 'isotope_grid',
    '#items' => $items,
    '#instance' => 1,
    '#attached' => [
      'css' => [
        drupal_get_path('module', 'views_isotope_example') . '/views_isotope_example.css',
      ],
    ],
  ];
  return $return;
}

/**
 * Implements hook_views_api().
 */
function views_isotope_example_views_api() {
  return [
    'api' => 3.0,
  ];
}

/**
 * Implements hook_preprocess_views_view().
 */
function views_isotope_example_preprocess_views_view(&$vars) {
  if ($vars['name'] == 'isotope_example') {
    drupal_add_css(drupal_get_path('module', 'views_isotope_example') . '/views_isotope_example.css');
  }
}

/**
 * Implements hook_node_info().
 */
function views_isotope_example_node_info() {
  return [
    'views_isotope_example' => [
      'name' => t('Isotope Example Node Type'),
      'base' => 'views_isotope_example',
      'description' => t('This is an example node type with a few fields.'),
      'title_label' => t('Example Title'),
      'locked' => TRUE,
    ],
  ];
}

/**
 * Implements hook_menu().
 */
function views_isotope_example_menu() {
  $items['isotope-example'] = [
    'title' => 'Isotope Examples',
    'page callback' => 'views_isotope_example_page',
    'access callback' => TRUE,
  ];
  $items['isotope-example/theme-example'] = [
    'title' => 'Isotope Theme Example',
    'page callback' => 'views_isotope_example_theme_page',
    'access callback' => TRUE,
  ];
  return $items;
}

/**
 * Page callback.
 *
 * @return array
 *   Drupal render array.
 */
function views_isotope_example_page() {
  $links[] = l(t('Theme Example'), 'isotope-example/theme-example');
  if (module_exists('views_isotope_views')) {
    $links[] = l(t('Views Example'), 'isotope-example/views-example');
  }
  $return[] = [
    '#theme' => 'item_list',
    '#items' => $links,
    '#type' => 'ul',
  ];
  return $return;
}