You are here

class HomeboxLayoutController in Homebox 8

Class HomeboxLayoutController.

Returns responses for Homebox Layout routes.

Hierarchy

Expanded class hierarchy of HomeboxLayoutController

File

src/Controller/HomeboxLayoutController.php, line 26

Namespace

Drupal\homebox\Controller
View source
class HomeboxLayoutController extends ControllerBase implements ContainerInjectionInterface {

  /**
   * Date formatter service.
   *
   * @var \Drupal\Core\Datetime\DateFormatterInterface
   */
  protected $dateFormatter;

  /**
   * Renderer service.
   *
   * @var \Drupal\Core\Render\RendererInterface
   */
  protected $renderer;

  /**
   * Manager block plugin.
   *
   * @var \Drupal\Core\Block\BlockManagerInterface
   */
  protected $blockManager;

  /**
   * The local action manager.
   *
   * @var \Drupal\Core\Menu\LocalActionManagerInterface
   */
  protected $localActionManager;

  /**
   * The route match.
   *
   * @var \Drupal\Core\Routing\RouteMatchInterface
   */
  protected $routeMatch;

  /**
   * The context repository.
   *
   * @var \Drupal\Core\Plugin\Context\LazyContextRepository
   */
  protected $contextRepository;

  /**
   * The link generator.
   *
   * @var \Drupal\Core\Utility\LinkGeneratorInterface
   */
  protected $linkGenerator;

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container) {

    /* @var \Drupal\Core\Datetime\DateFormatterInterface $date_formatter */
    $date_formatter = $container
      ->get('date.formatter');

    /* @var \Drupal\Core\Render\RendererInterface $renderer */
    $renderer = $container
      ->get('renderer');

    /* @var \Drupal\Core\Block\BlockManagerInterface $block_manager */
    $block_manager = $container
      ->get('plugin.manager.block');

    /* @var \Drupal\Core\Menu\LocalActionManagerInterface $local_action_manager */
    $local_action_manager = $container
      ->get('plugin.manager.menu.local_action');

    /* @var \Drupal\Core\Routing\RouteMatchInterface $route_match */
    $route_match = $container
      ->get('current_route_match');

    /* @var \Drupal\Core\Plugin\Context\LazyContextRepository $context_repository */
    $context_repository = $container
      ->get('context.repository');

    /** @var \Drupal\Core\Utility\LinkGeneratorInterface $link_generator */
    $link_generator = $container
      ->get('link_generator');
    return new static($date_formatter, $renderer, $block_manager, $local_action_manager, $route_match, $context_repository, $link_generator);
  }

  /**
   * HomeboxLayoutController constructor.
   *
   * @param \Drupal\Core\Datetime\DateFormatterInterface $date_formatter
   *   Date formatter service.
   * @param \Drupal\Core\Render\RendererInterface $renderer
   *   Renderer service.
   * @param \Drupal\Core\Block\BlockManagerInterface $block_manager
   *   Manager block plugin.
   * @param \Drupal\Core\Menu\LocalActionManagerInterface $local_action_manager
   *   The local action manager.
   * @param \Drupal\Core\Routing\RouteMatchInterface $route_match
   *   The current route match.
   * @param \Drupal\Core\Plugin\Context\LazyContextRepository $context_repository
   *   The context repository.
   * @param \Drupal\Core\Utility\LinkGeneratorInterface $link_generator
   *   The link generator service.
   */
  public function __construct(DateFormatterInterface $date_formatter, RendererInterface $renderer, BlockManagerInterface $block_manager, LocalActionManagerInterface $local_action_manager, RouteMatchInterface $route_match, LazyContextRepository $context_repository, LinkGeneratorInterface $link_generator) {
    $this->dateFormatter = $date_formatter;
    $this->renderer = $renderer;
    $this->blockManager = $block_manager;
    $this->localActionManager = $local_action_manager;
    $this->routeMatch = $route_match;
    $this->contextRepository = $context_repository;
    $this->linkGenerator = $link_generator;
  }

  /**
   * Displays a Homebox Layout  revision.
   *
   * @param int $homebox_layout_revision
   *   The Homebox Layout  revision ID.
   *
   * @return array
   *   An array suitable for drupal_render().
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function revisionShow($homebox_layout_revision) {
    $homebox_layout = $this
      ->entityTypeManager()
      ->getStorage('homebox_layout')
      ->loadRevision($homebox_layout_revision);
    $view_builder = $this
      ->entityTypeManager()
      ->getViewBuilder('homebox_layout');
    return $view_builder
      ->view($homebox_layout);
  }

  /**
   * Page title callback for a Homebox Layout  revision.
   *
   * @param int $homebox_layout_revision
   *   The Homebox Layout  revision ID.
   *
   * @return string
   *   The page title.
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   */
  public function revisionPageTitle($homebox_layout_revision) {

    /* @var HomeboxLayoutInterface $homebox_layout */
    $homebox_layout = $this
      ->entityTypeManager()
      ->getStorage('homebox_layout')
      ->loadRevision($homebox_layout_revision);
    return $this
      ->t('Revision of %title from %date', [
      '%title' => $homebox_layout
        ->label(),
      '%date' => $this->dateFormatter
        ->format($homebox_layout
        ->getRevisionCreationTime()),
    ]);
  }

  /**
   * Shows a list of blocks that can be added to a theme's layout.
   *
   * @param \Symfony\Component\HttpFoundation\Request $request
   *   The current request.
   * @param string $homebox
   *   Homebox id.
   *
   * @return array
   *   A render array as expected by the renderer.
   */
  public function listBlocks(Request $request, $homebox) {

    // Only add blocks which work without any available context.
    $definitions = $this->blockManager
      ->getDefinitions();

    // Order by category, and then by admin label.
    $definitions = $this->blockManager
      ->getSortedDefinitions($definitions);

    // Since modals do not render any other part of the page, we need to render
    // them manually as part of this listing.
    if ($request->query
      ->get(MainContentViewSubscriber::WRAPPER_FORMAT) === 'drupal_modal') {
      $build['local_actions'] = $this
        ->buildLocalActions();
    }
    $headers = [
      [
        'data' => $this
          ->t('Block'),
      ],
      [
        'data' => $this
          ->t('Category'),
      ],
      [
        'data' => $this
          ->t('Operations'),
      ],
    ];
    $region = $request->query
      ->get('region');
    $weight = $request->query
      ->get('weight');
    $rows = [];
    foreach ($definitions as $plugin_id => $plugin_definition) {
      $row = [];
      $row['title']['data'] = [
        '#type' => 'inline_template',
        '#template' => '<div class="block-filter-text-source">{{ label }}</div>',
        '#context' => [
          'label' => $plugin_definition['admin_label'],
        ],
      ];
      $row['category']['data'] = $plugin_definition['category'];
      $links['add'] = [
        'title' => $this
          ->t('Place block'),
        'url' => Url::fromRoute('homebox.settings_form', [
          'homebox' => $homebox,
        ], [
          'query' => [
            'block-placement' => $plugin_id,
          ],
        ]),
      ];
      if ($region) {
        $links['add']['query']['region'] = $region;
      }
      if (isset($weight)) {
        $links['add']['query']['weight'] = $weight;
      }
      $row['operations']['data'] = [
        '#type' => 'operations',
        '#links' => $links,
      ];
      $rows[] = $row;
    }
    $build['#attached']['library'][] = 'block/drupal.block.admin';
    $build['filter'] = [
      '#type' => 'search',
      '#title' => $this
        ->t('Filter'),
      '#title_display' => 'invisible',
      '#size' => 30,
      '#placeholder' => $this
        ->t('Filter by block name'),
      '#attributes' => [
        'class' => [
          'block-filter-text',
        ],
        'data-element' => '.block-add-table',
        'title' => $this
          ->t('Enter a part of the block name to filter by.'),
      ],
    ];
    $build['blocks'] = [
      '#type' => 'table',
      '#header' => $headers,
      '#rows' => $rows,
      '#empty' => $this
        ->t('No blocks available.'),
      '#attributes' => [
        'class' => [
          'block-add-table',
        ],
      ],
    ];
    return $build;
  }

  /**
   * Builds the local actions for this listing.
   *
   * @return array
   *   An array of local actions for this listing.
   */
  protected function buildLocalActions() {
    $build = $this->localActionManager
      ->getActionsForRoute($this->routeMatch
      ->getRouteName());

    // Without this workaround, the action links will be rendered as <li> with
    // no wrapping <ul> element.
    if (!empty($build)) {
      $build['#prefix'] = '<ul class="action-links">';
      $build['#suffix'] = '</ul>';
    }
    return $build;
  }

  /**
   * Generates an overview table of older revisions of a Homebox Layout .
   *
   * @param \Drupal\homebox\Entity\HomeboxLayoutInterface $homebox_layout
   *   A Homebox Layout  object.
   *
   * @return array
   *   An array as expected by drupal_render().
   *
   * @throws \Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException
   * @throws \Drupal\Component\Plugin\Exception\PluginNotFoundException
   * @throws \Drupal\Core\Entity\EntityMalformedException
   */
  public function revisionOverview(HomeboxLayoutInterface $homebox_layout) {
    $account = $this
      ->currentUser();
    $langcode = $homebox_layout
      ->language()
      ->getId();
    $langname = $homebox_layout
      ->language()
      ->getName();
    $languages = $homebox_layout
      ->getTranslationLanguages();
    $has_translations = count($languages) > 1;

    /* @var \Drupal\homebox\HomeboxLayoutStorageInterface $homebox_layout_storage */
    $homebox_layout_storage = $this
      ->entityTypeManager()
      ->getStorage('homebox_layout');
    $build['#title'] = $has_translations ? $this
      ->t('@langname revisions for %title', [
      '@langname' => $langname,
      '%title' => $homebox_layout
        ->label(),
    ]) : $this
      ->t('Revisions for %title', [
      '%title' => $homebox_layout
        ->label(),
    ]);
    $header = [
      $this
        ->t('Revision'),
      $this
        ->t('Operations'),
    ];
    $revert_permission = $account
      ->hasPermission('administer homebox');
    $delete_permission = $account
      ->hasPermission('administer homebox');
    $rows = [];
    $vids = $homebox_layout_storage
      ->revisionIds($homebox_layout);
    $latest_revision = TRUE;
    foreach (array_reverse($vids) as $vid) {

      /** @var \Drupal\homebox\Entity\HomeboxLayoutInterface $revision */
      $revision = $homebox_layout_storage
        ->loadRevision($vid);

      // Only show revisions that are affected by the language that is being
      // displayed.
      if ($revision
        ->hasTranslation($langcode)) {
        $username = [
          '#theme' => 'username',
          '#account' => $revision
            ->getRevisionUser(),
        ];

        // Use revision link to link to revisions that are not active.
        $date = $this->dateFormatter
          ->format($revision
          ->getRevisionCreationTime(), 'short');
        if ($vid != $homebox_layout
          ->getRevisionId()) {
          $link = $this->linkGenerator
            ->generate($date, new Url('entity.homebox_layout.revision', [
            'homebox_layout' => $homebox_layout
              ->id(),
            'homebox_layout_revision' => $vid,
          ]));
        }
        else {
          $link = $homebox_layout
            ->toLink($date)
            ->toString();
        }
        $row = [];
        $column = [
          'data' => [
            '#type' => 'inline_template',
            '#template' => '{% trans %}{{ date }} by {{ username }}{% endtrans %}{% if message %}<p class="revision-log">{{ message }}</p>{% endif %}',
            '#context' => [
              'date' => $link,
              'username' => $this->renderer
                ->renderPlain($username),
              'message' => [
                '#markup' => $revision
                  ->getRevisionLogMessage(),
                '#allowed_tags' => Xss::getHtmlTagList(),
              ],
            ],
          ],
        ];
        $row[] = $column;
        if ($latest_revision) {
          $row[] = [
            'data' => [
              '#prefix' => '<em>',
              '#markup' => $this
                ->t('Current revision'),
              '#suffix' => '</em>',
            ],
          ];
          foreach ($row as &$current) {
            $current['class'] = [
              'revision-current',
            ];
          }
          $latest_revision = FALSE;
        }
        else {
          $links = [];
          if ($revert_permission) {
            $links['revert'] = [
              'title' => $this
                ->t('Revert'),
              'url' => $has_translations ? Url::fromRoute('entity.homebox_layout.translation_revert', [
                'homebox_layout' => $homebox_layout
                  ->id(),
                'homebox_layout_revision' => $vid,
                'langcode' => $langcode,
              ]) : Url::fromRoute('entity.homebox_layout.revision_revert', [
                'homebox_layout' => $homebox_layout
                  ->id(),
                'homebox_layout_revision' => $vid,
              ]),
            ];
          }
          if ($delete_permission) {
            $links['delete'] = [
              'title' => $this
                ->t('Delete'),
              'url' => Url::fromRoute('entity.homebox_layout.revision_delete', [
                'homebox_layout' => $homebox_layout
                  ->id(),
                'homebox_layout_revision' => $vid,
              ]),
            ];
          }
          $row[] = [
            'data' => [
              '#type' => 'operations',
              '#links' => $links,
            ],
          ];
        }
        $rows[] = $row;
      }
    }
    $build['homebox_layout_revisions_table'] = [
      '#theme' => 'table',
      '#rows' => $rows,
      '#header' => $header,
    ];
    return $build;
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ControllerBase::$configFactory protected property The configuration factory.
ControllerBase::$currentUser protected property The current user service. 1
ControllerBase::$entityFormBuilder protected property The entity form builder.
ControllerBase::$entityManager protected property The entity manager.
ControllerBase::$entityTypeManager protected property The entity type manager.
ControllerBase::$formBuilder protected property The form builder. 2
ControllerBase::$keyValue protected property The key-value storage. 1
ControllerBase::$languageManager protected property The language manager. 1
ControllerBase::$moduleHandler protected property The module handler. 2
ControllerBase::$stateService protected property The state service.
ControllerBase::cache protected function Returns the requested cache bin.
ControllerBase::config protected function Retrieves a configuration object.
ControllerBase::container private function Returns the service container.
ControllerBase::currentUser protected function Returns the current user. 1
ControllerBase::entityFormBuilder protected function Retrieves the entity form builder.
ControllerBase::entityManager Deprecated protected function Retrieves the entity manager service.
ControllerBase::entityTypeManager protected function Retrieves the entity type manager.
ControllerBase::formBuilder protected function Returns the form builder service. 2
ControllerBase::keyValue protected function Returns a key/value storage collection. 1
ControllerBase::languageManager protected function Returns the language manager service. 1
ControllerBase::moduleHandler protected function Returns the module handler. 2
ControllerBase::redirect protected function Returns a redirect response object for the specified route. Overrides UrlGeneratorTrait::redirect
ControllerBase::state protected function Returns the state storage service.
HomeboxLayoutController::$blockManager protected property Manager block plugin.
HomeboxLayoutController::$contextRepository protected property The context repository.
HomeboxLayoutController::$dateFormatter protected property Date formatter service.
HomeboxLayoutController::$linkGenerator protected property The link generator. Overrides LinkGeneratorTrait::$linkGenerator
HomeboxLayoutController::$localActionManager protected property The local action manager.
HomeboxLayoutController::$renderer protected property Renderer service.
HomeboxLayoutController::$routeMatch protected property The route match.
HomeboxLayoutController::buildLocalActions protected function Builds the local actions for this listing.
HomeboxLayoutController::create public static function Instantiates a new instance of this class. Overrides ControllerBase::create
HomeboxLayoutController::listBlocks public function Shows a list of blocks that can be added to a theme's layout.
HomeboxLayoutController::revisionOverview public function Generates an overview table of older revisions of a Homebox Layout .
HomeboxLayoutController::revisionPageTitle public function Page title callback for a Homebox Layout revision.
HomeboxLayoutController::revisionShow public function Displays a Homebox Layout revision.
HomeboxLayoutController::__construct public function HomeboxLayoutController constructor.
LinkGeneratorTrait::getLinkGenerator Deprecated protected function Returns the link generator.
LinkGeneratorTrait::l Deprecated protected function Renders a link to a route given a route name and its parameters.
LinkGeneratorTrait::setLinkGenerator Deprecated public function Sets the link generator service.
LoggerChannelTrait::$loggerFactory protected property The logger channel factory service.
LoggerChannelTrait::getLogger protected function Gets the logger for a specific channel.
LoggerChannelTrait::setLoggerFactory public function Injects the logger channel factory.
MessengerTrait::$messenger protected property The messenger. 29
MessengerTrait::messenger public function Gets the messenger. 29
MessengerTrait::setMessenger public function Sets the messenger.
RedirectDestinationTrait::$redirectDestination protected property The redirect destination service. 1
RedirectDestinationTrait::getDestinationArray protected function Prepares a 'destination' URL query parameter for use with \Drupal\Core\Url.
RedirectDestinationTrait::getRedirectDestination protected function Returns the redirect destination service.
RedirectDestinationTrait::setRedirectDestination public function Sets the redirect destination service.
StringTranslationTrait::$stringTranslation protected property The string translation service. 1
StringTranslationTrait::formatPlural protected function Formats a string containing a count of items.
StringTranslationTrait::getNumberOfPlurals protected function Returns the number of plurals supported by a given language.
StringTranslationTrait::getStringTranslation protected function Gets the string translation service.
StringTranslationTrait::setStringTranslation public function Sets the string translation service to use. 2
StringTranslationTrait::t protected function Translates a string to the current language or to a given language.
UrlGeneratorTrait::$urlGenerator protected property The url generator.
UrlGeneratorTrait::getUrlGenerator Deprecated protected function Returns the URL generator service.
UrlGeneratorTrait::setUrlGenerator Deprecated public function Sets the URL generator service.
UrlGeneratorTrait::url Deprecated protected function Generates a URL or path for a specific route based on the given parameters.