You are here

protected function SubscriptionController::getView in Message Subscribe 8

Helper function to get a view associated with a flag.

Parameters

\Drupal\user\UserInterface $account: The user to pass in as the views argument.

\Drupal\flag\FlagInterface $flag: The flag for which to find a matching view.

Return value

\Drupal\views\ViewExecutable The corresponding view executable.

Throws

\Drupal\message_subscribe\Exception\MessageSubscribeException

  • If a view corresponding to the `subscribe_ENTITY_TYPE_ID` does not exist.
  • If the view's relationship flag isn't properly enabled or configured.
1 call to SubscriptionController::getView()
SubscriptionController::tab in message_subscribe_ui/src/Controller/SubscriptionController.php
Render the subscription management tab.

File

message_subscribe_ui/src/Controller/SubscriptionController.php, line 176

Class

SubscriptionController
Default controller for the message_subscribe_ui module.

Namespace

Drupal\message_subscribe_ui\Controller

Code

protected function getView(UserInterface $account, FlagInterface $flag) {
  $entity_type = $flag
    ->getFlaggableEntityTypeId();
  $prefix = $this->config
    ->get('flag_prefix');

  // View name + display ID.
  $default_view_name = $prefix . '_' . $entity_type . ':default';
  list($view_name, $display_id) = explode(':', $flag
    ->getThirdPartySetting('message_subscribe_ui', 'view_name', $default_view_name));
  if (!($view = Views::getView($view_name))) {

    // View doesn't exist.
    throw new MessageSubscribeException('View "' . $view_name . '" does not exist.');
  }
  $view
    ->setDisplay($display_id);
  $view
    ->setArguments([
    $account
      ->id(),
  ]);

  // Change the flag's relationship to point to our flag.
  $relationships = $view->display_handler
    ->getOption('relationships');
  foreach ($relationships as $key => $relationship) {
    if (strpos($key, 'flag_') !== 0) {

      // Not a flag relationship.
      continue;
    }

    // Check that the flag is valid.
    $rel_flag = $this->flagService
      ->getFlagById($relationship['flag']);
    if (!$rel_flag || !$rel_flag
      ->status()) {
      throw new MessageSubscribeException('Flag "' . $relationships['flag'] . '" is not setup correctly. It is probably disabled or have no bundles configured.');
    }
  }
  return $view;
}