schemadotorg_starterkit_hotel-1.0.x-dev/schemadotorg_starterkit_hotel.install
schemadotorg_starterkit_hotel.install
<?php
/**
* @file
* Installation hooks for the Schema.org Blueprints: Hotel Starter Kit module.
*/
declare(strict_types=1);
use Drupal\taxonomy\Entity\Term;
/**
* Implements hook_install().
*/
function schemadotorg_starterkit_hotel_install(bool $is_syncing): void {
/** @var \Drupal\schemadotorg\SchemaDotOrgNamesInterface $schema_names */
$schema_names = \Drupal::service('schemadotorg.names');
// Import amenities.
$csv_path = \Drupal::service('extension.list.module')->getPath('schemadotorg_starterkit_hotel') . '/data/amenities.csv';
$handle = fopen($csv_path, 'r');
fgetcsv($handle);
while ($data = fgetcsv($handle)) {
// Attribute.
$attribute = $data[0];
// Name.
$name = $attribute;
$name = str_replace('amenityFeature.', '', $name);
$name = (!preg_match('/^[A-Z]+$/', $name))
? $schema_names->camelCaseToSentenceCase($name)
: $name;
// Type.
$type = $data[2];
// Values.
$values = trim($data[3]);
$values = $values
? preg_split('/\s+/ims', $values)
: NULL;
// Description.
$description = $data[4];
// Skip strings which need to be figure out.
if ($type === 'string') {
continue;
}
if ($values) {
// Create multiple terms for multiple sub-values.
foreach ($values as $value) {
Term::create([
'name' => $name . ' - ' . ucfirst(strtolower($value)),
'description' => [
'value' => $description,
'format' => 'basic_html',
],
'vid' => 'amenity',
'schema_value' => $value,
])->save();
}
}
else {
// Create a single term.
Term::create([
'name' => $name,
'description' => [
'value' => $description,
'format' => 'basic_html',
],
'schema_value' => 'TRUE',
'vid' => 'amenity',
])->save();
}
}
fclose($handle);
// Create shortcuts.
if (\Drupal::moduleHandler()->moduleExists('shortcut')) {
/** @var \Drupal\shortcut\ShortcutSetStorageInterface $shortcut_storage */
$shortcut_storage = \Drupal::entityTypeManager()
->getStorage('shortcut');
$shortcuts = [
'internal:/hotels' => 'Hotels',
];
foreach ($shortcuts as $uri => $title) {
if (!$shortcut_storage->loadByProperties(['title' => $title])) {
$shortcut_storage->create([
'shortcut_set' => 'default',
'title' => $title,
'link' => ['uri' => $uri],
'weight' => -10,
])->save();
}
}
}
}
