A dummy module for testing custom block related hooks.
This is a dummy module that implements custom block related hooks to test API interaction with the custom_block module.
<?php
/**
* @file
* A dummy module for testing custom block related hooks.
*
* This is a dummy module that implements custom block related hooks to test API
* interaction with the custom_block module.
*/
use Drupal\custom_block\Plugin\Core\Entity\CustomBlock;
/**
* Implements hook_custom_block_load().
*/
function custom_block_test_custom_block_load($custom_blocks, $types) {
// Add properties to each loaded custom_block which record the parameters that
// were passed in to this function, so the tests can check that (a) this hook
// was called, and (b) the parameters were what we expected them to be.
$ids = array_keys($custom_blocks);
ksort($ids);
sort($types);
foreach ($custom_blocks as $custom_block) {
$custom_block->custom_block_test_loaded_ids = $ids;
$custom_block->custom_block_test_loaded_types = $types;
}
}
/**
* Implements hook_custom_block_view().
*/
function custom_block_test_custom_block_view(CustomBlock $custom_block, $view_mode) {
// Add extra content.
$custom_block->content['extra_content'] = array(
'#markup' => '<blink>Yowser</blink>',
);
}
/**
* Implements hook_custom_block_presave().
*/
function custom_block_test_custom_block_presave(CustomBlock $custom_block) {
if ($custom_block->info->value == 'testing_custom_block_presave') {
$custom_block->info->value .= '_presave';
}
// Determine changes.
if (!empty($custom_block->original) && $custom_block->original->info->value == 'test_changes') {
if ($custom_block->original->info->value != $custom_block->info->value) {
$custom_block->info->value .= '_presave';
}
}
}
/**
* Implements hook_custom_block_update().
*/
function custom_block_test_custom_block_update(CustomBlock $custom_block) {
// Determine changes on update.
if (!empty($custom_block->original) && $custom_block->original->info->value == 'test_changes') {
if ($custom_block->original->info->value != $custom_block->info->value) {
$custom_block->info->value .= '_update';
}
}
}
/**
* Implements hook_custom_block_insert().
*
* This tests saving a custom_block on custom_block insert.
*
* @see \Drupal\custom_block\Tests\CustomBlockSaveTest::testCustomBlockSaveOnInsert()
*/
function custom_block_test_custom_block_insert(CustomBlock $custom_block) {
// Set the custom_block title to the custom_block ID and save.
if ($custom_block->info->value == 'new') {
$custom_block->info->value = 'CustomBlock ' . $custom_block->id->value;
$custom_block
->save();
}
if ($custom_block->info->value == 'fail_creation') {
throw new Exception('Test exception for rollback.');
}
}
/**
* Implements hook_menu().
*/
function custom_block_test_menu() {
$items = array();
// Add a block-view callback.
$items['custom-block/%custom_block'] = array(
'title callback' => 'entity_page_label',
'title arguments' => array(
1,
),
'page callback' => 'entity_view',
'page arguments' => array(
1,
'default',
),
'access callback' => 'entity_page_access',
'access arguments' => array(
1,
'view',
),
);
return $items;
}
Name | Description |
---|---|
custom_block_test_custom_block_insert | Implements hook_custom_block_insert(). |
custom_block_test_custom_block_load | Implements hook_custom_block_load(). |
custom_block_test_custom_block_presave | Implements hook_custom_block_presave(). |
custom_block_test_custom_block_update | Implements hook_custom_block_update(). |
custom_block_test_custom_block_view | Implements hook_custom_block_view(). |
custom_block_test_menu | Implements hook_menu(). |