CustomBlockController.php 3.53 KB
Newer Older
1 2 3 4 5 6 7 8 9
<?php

/**
 * @file
 * Contains \Drupal\custom_block\Controller\CustomBlockController
 */

namespace Drupal\custom_block\Controller;

10
use Drupal\Core\Controller\ControllerInterface;
11
use Drupal\Core\Entity\EntityStorageControllerInterface;
12
use Drupal\custom_block\CustomBlockTypeInterface;
13 14 15 16 17 18 19 20 21 22 23 24 25
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;

class CustomBlockController implements ControllerInterface {

  /**
   * Current request object.
   *
   * @var \Symfony\Component\HttpFoundation\Request
   */
  protected $request;

  /**
26
   * The custom block storage controller.
27
   *
28
   * @var \Drupal\Core\Entity\EntityStorageControllerInterface
29
   */
30
  protected $customBlockStorage;
31 32

  /**
33
   * The custom block type storage controller.
34
   *
35
   * @var \Drupal\Core\Entity\EntityStorageControllerInterface
36
   */
37
  protected $customBlockTypeStorage;
38 39 40 41 42

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {
43
    $entity_manager = $container->get('plugin.manager.entity');
44 45
    return new static(
      $container->get('request'),
46 47
      $entity_manager->getStorageController('custom_block'),
      $entity_manager->getStorageController('custom_block_type')
48 49 50 51 52 53 54 55
    );
  }

  /**
   * Constructs a CustomBlock object.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   Current request.
56
   * @param \Drupal\Core\Entity\EntityStorageControllerInterface $custom_block_storage
57
   *   The custom block storage controller.
58
   * @param \Drupal\Core\Entity\EntityStorageControllerInterface $custom_block_type_storage
59
   *   The custom block type storage controller.
60
   */
61
  public function __construct(Request $request, EntityStorageControllerInterface $custom_block_storage, EntityStorageControllerInterface $custom_block_type_storage) {
62
    $this->request = $request;
63 64
    $this->customBlockStorage = $custom_block_storage;
    $this->customBlockTypeStorage = $custom_block_type_storage;
65 66 67 68 69 70 71 72 73 74 75
  }

  /**
   * Displays add custom block links for available types.
   *
   * @return array
   *   A render array for a list of the custom block types that can be added or
   *   if there is only one custom block type defined for the site, the function
   *   returns the custom block add page for that custom block type.
   */
  public function add() {
76
    $types = $this->customBlockTypeStorage->load();
77 78
    if ($types && count($types) == 1) {
      $type = reset($types);
79
      return $this->addForm($type);
80 81 82 83 84
    }

    return array('#theme' => 'custom_block_add_list', '#content' => $types);
  }

85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
  /**
   * Presents the custom block creation form.
   *
   * @param \Drupal\custom_block\CustomBlockTypeInterface $custom_block_type
   *   The custom block type to add.
   *
   * @return array
   *   A form array as expected by drupal_render().
   */
  public function addForm(CustomBlockTypeInterface $custom_block_type) {
    // @todo Remove this when https://drupal.org/node/1981644 is in.
    drupal_set_title(t('Add %type custom block', array(
      '%type' => $custom_block_type->label()
    )), PASS_THROUGH);
    $block = $this->customBlockStorage->create(array(
      'type' => $custom_block_type->id()
    ));
    if (($theme = $this->request->attributes->get('theme')) && in_array($theme, array_keys(list_themes()))) {
      // We have navigated to this page from the block library and will keep track
      // of the theme for redirecting the user to the configuration page for the
      // newly created block in the given theme.
      $block->setTheme($theme);
    }
    return entity_get_form($block);
  }

111
}