You are here

private function ActionLinkController::generateResponse in Flag Lists 4.0.x

Same name and namespace in other branches
  1. 8 src/Controller/ActionLinkController.php \Drupal\flag_lists\Controller\ActionLinkController::generateResponse()

Generates a response after the flag has been updated.

The response is different from the Flag modules response as we add the flagging collection Id as well.

Parameters

\Drupal\flag\FlagInterface $flag: The flag entity.

\Drupal\Core\Entity\EntityInterface $entity: The entity object.

string $flag_list: The flag list from the link.

string $message: (optional) The message to flash.

Return value

\Drupal\Core\Ajax\AjaxResponse|\Symfony\Component\HttpFoundation\RedirectResponse The response object.

See also

\Drupal\flag_lists\Entity\FlaggingCollection

2 calls to ActionLinkController::generateResponse()
ActionLinkController::flag in src/Controller/ActionLinkController.php
Performs a flagging when called via a route.
ActionLinkController::unflag in src/Controller/ActionLinkController.php
Performs a unflagging when called via a route.

File

src/Controller/ActionLinkController.php, line 174

Class

ActionLinkController
Controller responses to flag and unflag action links.

Namespace

Drupal\flag_lists\Controller

Code

private function generateResponse(FlagInterface $flag, EntityInterface $entity, $flag_list, $message) {

  // Create a new AJAX response.
  $response = new AjaxResponse();

  // Get the link type plugin.
  $link_type = $flag
    ->getLinkTypePlugin();

  // Generate the link render array.
  $link = $link_type
    ->getAsFlagLink($flag, $entity);
  $flc = $this->flagListsService
    ->getFlaggingCollectionById($flag_list);
  $token_service = \Drupal::token();
  $bubbleable_metadata = new BubbleableMetadata();
  $link['#title']['#markup'] = $token_service
    ->replace($link['#title']['#markup'], [
    'flagging_collection' => $flc,
  ], [], $bubbleable_metadata);
  $bubbleable_metadata
    ->applyTo($link);
  $link['#flagging_collection'] = $flag_list;

  // Add the flag_list to the link
  // This is a messy method where everything is made by hand...
  $options = UrlHelper::parse($link['#attributes']['href']);
  $uri = 'internal:' . $options['path'] . '/' . $flag_list;
  $path = ltrim($options['path'] . '/' . $flag_list, '/');

  // Create a token.
  $generator = \Drupal::csrfToken();
  $token = $generator
    ->get($path);
  unset($options['path']);
  $options['query']['token'] = $token;

  // Update the href.
  $link['#attributes']['href'] = Url::fromUri($uri, $options)
    ->toString();

  // Generate a CSS selector to use in a JQuery Replace command.
  $selector = '.js-flag-' . Html::cleanCssIdentifier($flag
    ->id()) . '-' . $entity
    ->id() . '-' . $flag_list;

  // Create a new JQuery Replace command to update the link display.
  $replace = new ReplaceCommand($selector, $this->renderer
    ->renderPlain($link));
  $response
    ->addCommand($replace);

  // Push a message pulsing command onto the stack.
  $pulse = new ActionLinkFlashCommand($selector, $message);
  $response
    ->addCommand($pulse);
  return $response;
}