You are here

views_isotope_example.install in Brainstorm profile 7

Create sample data to be used in isotope example.

File

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

/**
 * @file
 * Create sample data to be used in isotope example.
 */

/**
 * Implements hook_install().
 *
 * Create sample data.
 */
function views_isotope_example_install() {
  $fields = [
    'views_isotope_example_color' => [
      'field_name' => 'views_isotope_example_color',
      'cardinality' => 1,
      'type' => 'text',
    ],
    'views_isotope_example_shape' => [
      'field_name' => 'views_isotope_example_shape',
      'cardinality' => 1,
      'type' => 'text',
    ],
    'views_isotope_example_size' => [
      'field_name' => 'views_isotope_example_size',
      'cardinality' => 1,
      'type' => 'text',
    ],
  ];
  $instances = [
    'views_isotope_example_color' => [
      'field_name' => 'views_isotope_example_color',
      'label' => t('Color'),
      'type' => 'text',
      'widget' => [
        'type' => 'text_textfield',
      ],
    ],
    'views_isotope_example_shape' => [
      'field_name' => 'views_isotope_example_shape',
      'label' => t('Shape'),
      'type' => 'text',
      'widget' => [
        'type' => 'text_textfield',
      ],
    ],
    'views_isotope_example_size' => [
      'field_name' => 'views_isotope_example_size',
      'label' => t('Size'),
      'type' => 'text',
      'widget' => [
        'type' => 'text_textfield',
      ],
    ],
  ];

  // Create all the fields we are adding to our content type.
  foreach ($fields as $field) {
    field_create_field($field);
  }

  // Create all the instances for our fields.
  foreach ($instances as $instance) {
    $instance['entity_type'] = 'node';
    $instance['bundle'] = 'views_isotope_example';
    field_create_instance($instance);
  }
  $colors = [
    'Blue',
    'Red',
    'Yellow',
  ];
  $sizes = [
    'Small',
    'Wide',
    'Big',
    'Tall',
  ];
  $shapes = [
    'Round',
    'Square',
  ];

  // Create nodes of every size shape and color.
  $items = [];
  $i = 0;
  foreach ($sizes as $size) {
    foreach ($shapes as $shape) {
      foreach ($colors as $color) {
        $node = new stdClass();
        $node->type = 'views_isotope_example';
        $node->title = t('Sample !i', [
          '!i' => $i,
        ]);
        $node->language = LANGUAGE_NONE;
        node_object_prepare($node);
        $node->views_isotope_example_size[$node->language][]['value'] = $size;
        $node->views_isotope_example_shape[$node->language][]['value'] = $shape;
        $node->views_isotope_example_color[$node->language][]['value'] = $color;
        $node = node_submit($node);
        node_save($node);
        $i++;
      }
    }
  }
}

/**
 * Implements hook_uninstall().
 *
 * Delete sample data.
 */
function views_isotope_example_uninstall() {

  // Delete the example nodes.
  $query = new EntityFieldQuery();
  $query
    ->entityCondition('entity_type', 'node')
    ->propertyCondition('type', 'views_isotope_example');
  $result = $query
    ->execute();
  if (!empty($result['node'])) {
    foreach ($result['node'] as $node) {
      node_delete($node->nid);
    }
  }

  // Delete the example fields.
  $fields = [
    'views_isotope_example_color',
    'views_isotope_example_shape',
    'views_isotope_example_size',
  ];
  foreach ($fields as $field_name) {
    field_delete_field($field_name);
  }
}