Hook “More” Link In Views For Drupal 8

PROBLEM
I needed a way to modify the Custom URL in the Pager for the More link for Views. I added a Contextual Filter so that the particular display can be put onto any page and it will display items from the current office.

This URL needs to be dynamic and change depending on where on the site this view is placed.

What Views needs here is some tokens!

SOLUTION
I looked into hooking “container” but that seems messy as I’m not sure where else it’s used. It’s doable that way but I primarily tried to look at the view.

In my preprocess, THEME_views_pre_render, I tried to dig further into the ViewsExecutable class. It turns out, the display_plugin method was what I was looking for.

Here’s some of my code:

function THEME_views_pre_render(\Drupal\views\ViewExecutable $view) {
	// target your view and display
	if (($view->id() == "my_view") && ($view->current_display == "my_display")) {
		// this is just what's actually in the field to begin with... i.e. "success-story" in my screen shot above
		$ending_part_of_uri = $view->display_handler->getOption("link_url");

		// call a function or whatever to get the part of your uri
		$starting_part_of_uri = _THEME_function_to_dynamically_get_URI();

		// now set the link to whatever
		$view->display_handler->setOption("link_url", $starting_part_of_uri . $ending_part_of_uri));
	}
}

Leave a Reply

Your email address will not be published. Required fields are marked *