advent_calendar-1.0.0-beta5/advent_calendar_quickstart/advent_calendar_quickstart.install

advent_calendar_quickstart/advent_calendar_quickstart.install
<?php

/**
 * @file
 * Install, update and uninstall functions for the Bundle Converter module.
 */

use Drupal\Core\File\FileSystemInterface;
use Drupal\node\Entity\Node;
use Drupal\taxonomy\Entity\Term;

/**
 * Implements hook_install().
 */
function advent_calendar_quickstart_install($is_syncing) {
  $year = date("Y");
  $terms = \Drupal::entityTypeManager()->getStorage('taxonomy_term')->loadByProperties([
    'vid' => 'year',
    'name' => $year,
  ]);
  if ($terms) {
    $tid = array_key_first($terms);
  }
  else {
    $tid = Term::create([
      'vid' => 'year',
      'name' => $year,
    ])->save();
  }
  // Make an array of random door positions.
  $positions = range(1, 23);
  shuffle($positions);
  array_push($positions, 24);
  // Set up a file var, but don't populate until needed.
  $file = FALSE;
  foreach ($positions as $key => $position) {
    $day = $key + 1;
    $nodes = \Drupal::entityTypeManager()->getStorage('node')->loadByProperties([
      'type' => 'advent_calendar_door',
      'field_year' => $tid,
      'field_day' => $day,
    ]);
    if (!$nodes) {
      if (!$file) {
        $path = \Drupal::service('extension.path.resolver')
          ->getPath('module', 'advent_calendar_quickstart');
        $temp_file = file_get_contents($path . '/images/candle.png');
        $file = \Drupal::service('file.repository')->writeData($temp_file, 'public://candle.png', FileSystemInterface::EXISTS_RENAME);
      }
      Node::create([
        'type' => 'advent_calendar_door',
        'title' => t('Advent Calendar day @day', ['@day' => $day]),
        'field_brief_title' => 'Candle',
        'field_day' => $day,
        'field_year' => $tid,
        'field_position' => $position,
        'field_door_image' => [
          'target_id' => $file->id(),
          'alt' => 'Candle',
          'title' => 'Candle',
        ],
        'status' => 0,
      ])->save();
    }
  }
}

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

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