0

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?

Gilbot
  • 21
  • 4
  • Have you tried the [context](https://drupal.org/project/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 Jun 10 '13 at 14:13

2 Answers2

0

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']);;
?>
Hugh Wormington
  • 305
  • 1
  • 2
  • 10
  • 1
    You could do it this way, but I've heard the PHP Filter module can create some vulnerability to the site. – inertialmedia Jun 10 '13 at 14:12
  • That's poss of course if you add it to a text format (don't). Or if you do silly things in your PHP snippets. But I don't see a problem adding php to a block filter. e.g. Orr you could try [Extended Block Visibility](https://drupal.org/project/extended_block_visibility), but it requires you to implement a custom module. – Hugh Wormington Jun 11 '13 at 09:19
0

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 /).

Alex Skrypnyk
  • 1,331
  • 13
  • 12