0
votes

I have created a view page which shows all the work project thumbnails by default and then there is a block included on the page which filters the view (but not using ajax) it just added the company name into the url and it filters using contexual filters. The thumbnails in the grid take you to the project node page. I would like to include the filter sidebar block on all of the urls under www.sitename.com/work, so work & work/companyname/ but not on the node page which is www.sitename.com/work/companyname/projectname.

I have tried all possible ways of doing it within the path field.

work
work/*
work/*/~
work/*/~/
work/*/~/~

Is there anyway to include this block on all paths work/companyname but not any deeper?

2
Have you tried the context module? It allows you to display blocks according to numerous aspects of your Drupal site. Therefore, you can make a condition specific to the pages you want it to show on - and attach it to that context... - inertialmedia

2 Answers

0
votes

You could enable the core PHP Filter module. Then you can use php to set the block visibility e.g. using preg_match().

If you're not sure about regex take a look at http://www.regextester.com/.

EG to show block in admin and admin/structure but not admin/structure/blocks etc:

<?php 
  return preg_match('/^admin(\/structure)?(\/)?$/', $_GET['q']);;
?>
0
votes

What you are looking for is support for globbing (https://github.com/begin/globbing#wildcards). Unfortunately, Drupal does not support globbing out of the box.

In modern globbing implementation, * would match on any character but /, and ** would match on any character, including /.

In order to implement this support, one would need to:

  1. Look how PathMatcher (core/lib/Drupal/Core/Path/PathMatcher.php) service matches the path.

  2. Extend it into own custom service where only matchPath() will be overridden.

  3. Replace the content of matchPath() with the code below (this is a copy of the original matchPath() with some alterations).

  4. Alter included service to use your custom path matching (for block only or whole site).

  5. Update configuration of blocks to use ** for full path matching and * for subpath only.

/**
 * {@inheritdoc}
 */
public function matchPath($path, $patterns) {

  if (!isset($this->regexes[$patterns])) {
    // Convert path settings to a regular expression.
    $to_replace = [
      // Replace newlines with a logical 'or'.
      '/(\r\n?|\n)/',
      '/\\\\\*\\\\\*/',
      // Quote asterisks.
      '/\\\\\*/',
      // Quote <front> keyword.
      '/(^|\|)\\\\<front\\\\>($|\|)/',
    ];
    $replacements = [
      '|',
      '.*',
      '[^\/]*',
      '\1' . preg_quote($this->getFrontPagePath(), '/') . '\2',
    ];
    $patterns_quoted = preg_quote($patterns, '/');
    $this->regexes[$patterns] = '/^(' . preg_replace($to_replace, $replacements, $patterns_quoted) . ')$/';
  }
  return (bool) preg_match($this->regexes[$patterns], $path);
}   

Note that this code only adds additional token replacement ** and alters what * token does (any character but /).