You are here

bef_test.install in Better Exposed Filters 8.3

File

tests/bef_test/bef_test.install
View source
<?php

/**
 * Provides install hooks for the BEF Test module.
 */
use Drupal\taxonomy\Entity;

/**
 * Adds terms to the hierarchical "location" vocabulary.
 */
function bef_test_install() {

  // Set up an example hierarchical terms in the "Location" vocab.
  $locations = array(
    'United States' => array(
      'California' => array(
        'San Francisco',
        'San Diego',
        'Santa Barbara',
      ),
      'Oregon' => array(
        'Portland',
        'Eugene',
      ),
      'Washington' => array(
        'Seattle',
        'Spokane',
        'Walla Walla',
      ),
    ),
    'Canada' => array(
      'British Columbia' => array(
        'Vancouver',
        'Victoria',
        'Whistler',
      ),
      'Alberta' => array(
        'Calgary',
        'Edmonton',
        'Lake Louise',
      ),
    ),
    'Mexico' => array(),
  );
  foreach ($locations as $country => $states) {
    $country_tid = _bef_test_add_term($country);
    if ($country_tid && !empty($states)) {
      foreach ($states as $state => $cities) {
        $state_tid = _bef_test_add_term($state, $country_tid);
        if ($state_tid && !empty($cities)) {
          foreach ($cities as $city) {
            _bef_test_add_term($city, $state_tid);
          }
        }
      }
    }
  }
}

/**
 * Adds a new term to the bef_test-location vocabulary. If a TID is specified
 * in $parent, the new term is added as a child of that term.
 *
 * @param string $name
 *   The name of the new term.
 * @param int $parent
 *   The (optional) TID of the parent term.
 *
 * @return int
 *   TID of the newly created term.
 */
function _bef_test_add_term($name, $parent = 0) {
  $term = \Drupal::entityTypeManager()
    ->getStorage('taxonomy_term')
    ->create([
    'vid' => 'bef_test_location',
    'name' => $name,
    'parent' => [
      $parent,
    ],
  ]);
  $term
    ->save();
  return $term
    ->id();
}

Functions

Namesort descending Description
bef_test_install Adds terms to the hierarchical "location" vocabulary.
_bef_test_add_term Adds a new term to the bef_test-location vocabulary. If a TID is specified in $parent, the new term is added as a child of that term.