jquery_carousel_examples.install in jQuery carousel 8
Adds dummy data for jquery carousel examples.
File
jquery_carousel_examples/jquery_carousel_examples.installView source
<?php
/**
* @file
* Adds dummy data for jquery carousel examples.
*/
use Drupal\node\Entity\Node;
use Drupal\menu_link_content\Entity\MenuLinkContent;
use Drupal\Core\File\FileSystemInterface;
use Drupal\node\NodeInterface;
/**
* Implements hook_install().
*/
function jquery_carousel_examples_install() {
jquery_carousel_examples_generate_content();
}
/**
* Generate the sample content for views.
*/
function jquery_carousel_examples_generate_content() {
$dir = drupal_get_path('module', 'jquery_carousel_examples') . '/assets/images';
$images = FileSystemInterface::scanDirectory($dir, '/jquery_carousel-sample/');
// Create single node with multi-valued data & add it to menu.
foreach ($images as $image) {
$filename = $image->filename;
$file_temp = file_get_contents($image->uri);
$file_temp = file_save_data($file_temp, 'public://' . $filename, FileSystemInterface::EXISTS_RENAME);
$files[] = [
'target_id' => $file_temp
->id(),
];
}
jquery_carousel_create_node($files, $image, TRUE);
// Create separate nodes to be used by views.
foreach ($images as $image) {
$filename = $image->filename;
$file_temp = file_get_contents($image->uri);
$file_temp = file_save_data($file_temp, 'public://' . $filename, FileSystemInterface::EXISTS_RENAME);
$files = [
'target_id' => $file_temp
->id(),
];
jquery_carousel_create_node($files, $image);
}
}
function jquery_carousel_create_node($files, $image, $add_to_menu = FALSE) {
// Create node object with attached file.
$node = Node::create([
'type' => 'jquery_carousel_example',
'title' => ucfirst(str_replace('-', ' ', $image->name)),
'promote' => NodeInterface::NOT_PROMOTED,
'created' => \Drupal::time()
->getRequestTime(),
'changed' => \Drupal::time()
->getRequestTime(),
'uid' => 1,
'field_jquery_carousel_image' => $files,
]);
try {
$result = $node
->save();
if ($add_to_menu) {
// Add a link to the page in jquery carousel example menu.
MenuLinkContent::create([
'title' => 'Multi-valued field widget Example',
'link' => [
'uri' => 'internal:/node/' . $node
->id(),
],
'menu_name' => 'jquery-carousel-examples',
'enabled' => 1,
])
->save();
}
if ($result == SAVED_NEW) {
$saved = 'Created';
}
elseif ($result == SAVED_UPDATED) {
$saved = 'Updated';
}
else {
throw new Exception(t('Unknown value [@result] was returned', array(
'@result' => $result,
)));
}
\Drupal::logger('jquery_carousel_examples')
->info(t('@saved jquery_carousel_example node @nid', array(
'@saved' => $saved,
'@nid' => $node
->id(),
)), array());
} catch (Exception $e) {
\Drupal::logger('jquery_carousel_examples')
->error(t('Node create failure: @msg', array(
'@msg' => $e
->getMessage(),
)), array());
}
}
/**
* Implements hook_uninstall().
*/
function jquery_carousel_examples_uninstall() {
// Collect all of the example nodes and delete them.
$storage_handler = \Drupal::entityTypeManager()
->getStorage('node');
$nodes = $storage_handler
->loadByProperties(array(
'type' => 'jquery_carousel_example',
));
// Ensure we have some nodes before deleting.
if (!empty($nodes)) {
$storage_handler
->delete($nodes);
\Drupal::logger('jquery_carousel_examples')
->info(t('Deleted @count jquery_carousel_example nodes', array(
'@count' => count($nodes),
)), array());
}
// Now delete all of the configuration installed by this module.
$dir = drupal_get_path('module', 'jquery_carousel_examples') . '/config/install';
$files = FileSystemInterface::scanDirectory($dir, '/.*/');
foreach ($files as $file) {
\Drupal::configFactory()
->getEditable($file->name)
->delete();
}
\Drupal::logger('jquery_carousel_examples')
->info(t('Deleted jquery_carousel_example configuration'), array());
}
Functions
Name | Description |
---|---|
jquery_carousel_create_node | |
jquery_carousel_examples_generate_content | Generate the sample content for views. |
jquery_carousel_examples_install | Implements hook_install(). |
jquery_carousel_examples_uninstall | Implements hook_uninstall(). |