block_content_test.module 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /**
  3. * @file
  4. * A dummy module for testing custom block related hooks.
  5. *
  6. * This is a dummy module that implements custom block related hooks to test API
  7. * interaction with the block_content module.
  8. */
  9. use Drupal\block_content\Entity\BlockContent;
  10. /**
  11. * Implements hook_block_content_view().
  12. */
  13. function block_content_test_block_content_view(array &$build, BlockContent $block_content, $view_mode) {
  14. // Add extra content.
  15. $build['extra_content'] = [
  16. '#markup' => '<blink>Yowser</blink>',
  17. ];
  18. }
  19. /**
  20. * Implements hook_block_content_presave().
  21. */
  22. function block_content_test_block_content_presave(BlockContent $block_content) {
  23. if ($block_content->label() == 'testing_block_content_presave') {
  24. $block_content->setInfo($block_content->label() . '_presave');
  25. }
  26. // Determine changes.
  27. if (!empty($block_content->original) && $block_content->original->label() == 'test_changes') {
  28. if ($block_content->original->label() != $block_content->label()) {
  29. $block_content->setInfo($block_content->label() . '_presave');
  30. // Drupal 1.0 release.
  31. $block_content->changed = 979534800;
  32. }
  33. }
  34. }
  35. /**
  36. * Implements hook_block_content_update().
  37. */
  38. function block_content_test_block_content_update(BlockContent $block_content) {
  39. // Determine changes on update.
  40. if (!empty($block_content->original) && $block_content->original->label() == 'test_changes') {
  41. if ($block_content->original->label() != $block_content->label()) {
  42. $block_content->setInfo($block_content->label() . '_update');
  43. }
  44. }
  45. }
  46. /**
  47. * Implements hook_block_content_insert().
  48. *
  49. * This tests saving a block_content on block_content insert.
  50. *
  51. * @see \Drupal\block_content\Tests\BlockContentSaveTest::testBlockContentSaveOnInsert()
  52. */
  53. function block_content_test_block_content_insert(BlockContent $block_content) {
  54. // Set the block_content title to the block_content ID and save.
  55. if ($block_content->label() == 'new') {
  56. $block_content->setInfo('BlockContent ' . $block_content->id());
  57. $block_content->setNewRevision(FALSE);
  58. $block_content->save();
  59. }
  60. if ($block_content->label() == 'fail_creation') {
  61. throw new Exception('Test exception for rollback.');
  62. }
  63. }