add_instruction_block.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. <?php
  2. use Drupal\block_content\Entity\BlockContent;
  3. use Drupal\block\Entity\Block;
  4. // 1. Loome või laadime basic block_content juhendtekstiga.
  5. $text = '<div>Esmakordsel sisenemisel vajuta kõigepealt nuppu "soovin uut parooli". Seejärel sisesta lahtitulnud lehele aadress, mille oled liidule oma kontaktina andnud. Ühekordne parool saadetakse ainult sellele mailile. Peale sisselogimist saad kontaktaadressi ja parooli oma soovi kohaselt muuta. Kontrolli igaks juhuks ka rämpsposti</div>';
  6. // Otsi olemasolevat block_content info="Anonüümse avalehe juhend" järgi.
  7. $bc = NULL;
  8. $ids = \Drupal::entityQuery('block_content')
  9. ->condition('type', 'basic')
  10. ->condition('info', 'Anonüümse avalehe juhend')
  11. ->accessCheck(FALSE)
  12. ->execute();
  13. if ($ids) {
  14. $bc = BlockContent::load(reset($ids));
  15. echo "USING EXISTING block_content id=" . $bc->id() . PHP_EOL;
  16. } else {
  17. $bc = BlockContent::create([
  18. 'type' => 'basic',
  19. 'info' => 'Anonüümse avalehe juhend',
  20. 'body' => [
  21. 'value' => $text,
  22. 'format' => 'filtered_html',
  23. ],
  24. ]);
  25. $bc->save();
  26. echo "CREATED block_content id=" . $bc->id() . PHP_EOL;
  27. }
  28. // 2. Loome blocki, mis seda kuvab anonüümsele content regionis.
  29. $block_id = 'anonymous_front_instruction';
  30. $existing = Block::load($block_id);
  31. if ($existing) {
  32. echo "BLOCK EXISTS: $block_id" . PHP_EOL;
  33. } else {
  34. $block = Block::create([
  35. 'id' => $block_id,
  36. 'theme' => 'olivero',
  37. 'region' => 'content',
  38. 'weight' => -10,
  39. 'status' => TRUE,
  40. 'provider' => 'block_content',
  41. 'plugin' => 'block_content:' . $bc->uuid(),
  42. 'settings' => [
  43. 'id' => 'block_content:' . $bc->uuid(),
  44. 'label' => 'Anonüümse avalehe juhend',
  45. 'label_display' => '0',
  46. 'provider' => 'block_content',
  47. 'view_mode' => 'full',
  48. ],
  49. 'dependencies' => [
  50. 'content' => ['block_content:basic:' . $bc->uuid()],
  51. 'module' => ['block_content'],
  52. 'theme' => ['olivero'],
  53. ],
  54. 'visibility' => [
  55. 'user_role' => [
  56. 'id' => 'user_role',
  57. 'negate' => FALSE,
  58. 'context_mapping' => [
  59. 'user' => '@user.current_user_context:current_user',
  60. ],
  61. 'roles' => ['anonymous' => 'anonymous'],
  62. ],
  63. ],
  64. ]);
  65. $block->save();
  66. echo "CREATED BLOCK $block_id" . PHP_EOL;
  67. }