block_content_test.module 2.16 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
<?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 block_content module.
 */

use Drupal\block_content\Entity\BlockContent;

/**
 * Implements hook_block_content_view().
 */
function block_content_test_block_content_view(array &$build, BlockContent $block_content, $view_mode) {
  // Add extra content.
  $build['extra_content'] = array(
    '#markup' => '<blink>Yowser</blink>',
  );
}

/**
 * Implements hook_block_content_presave().
 */
function block_content_test_block_content_presave(BlockContent $block_content) {
  if ($block_content->label() == 'testing_block_content_presave') {
28
    $block_content->setInfo($block_content->label() . '_presave');
29 30 31 32
  }
  // Determine changes.
  if (!empty($block_content->original) && $block_content->original->label() == 'test_changes') {
    if ($block_content->original->label() != $block_content->label()) {
33
      $block_content->setInfo($block_content->label() . '_presave');
34 35 36 37 38 39 40 41 42 43 44 45 46
      // Drupal 1.0 release.
      $block_content->changed = 979534800;
    }
  }
}

/**
 * Implements hook_block_content_update().
 */
function block_content_test_block_content_update(BlockContent $block_content) {
  // Determine changes on update.
  if (!empty($block_content->original) && $block_content->original->label() == 'test_changes') {
    if ($block_content->original->label() != $block_content->label()) {
47
      $block_content->setInfo($block_content->label() . '_update');
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    }
  }
}

/**
 * Implements hook_block_content_insert().
 *
 * This tests saving a block_content on block_content insert.
 *
 * @see \Drupal\block_content\Tests\BlockContentSaveTest::testBlockContentSaveOnInsert()
 */
function block_content_test_block_content_insert(BlockContent $block_content) {
  // Set the block_content title to the block_content ID and save.
  if ($block_content->label() == 'new') {
    $block_content->setInfo('BlockContent ' . $block_content->id());
63
    $block_content->setNewRevision(FALSE);
64 65 66 67 68 69
    $block_content->save();
  }
  if ($block_content->label() == 'fail_creation') {
    throw new Exception('Test exception for rollback.');
  }
}