0

It seems like I could utilize the getVisibilityConditions method on the block, which returns a ConditionPluginCollection object with a setInstanceConfiguration method, which is apparently easy enough to use, requiring $request_path and $configuration arguments. But, the current block visibility request path variable is a string, containing paths separated by commas. How can I update that to either add or remove paths on node save? I know that I'm essentially asking how to manipulate block visibility on the node form, which isn't completely wrong. It just seems like I could be missing something in the API that would allow this kind if thing.

4
  • Is this the answer to your question? drupal.stackexchange.com/a/226037/47547 - I don't want to vote for a duplicate because the question over there is already marked as a false duplicate.
    – 4uk4
    May 10, 2022 at 16:31
  • @4uk4 it’s open now (your reopen vote is binding because of your score in the related tags)
    – Clive
    May 10, 2022 at 16:39
  • @4uk4 your answer at drupal.stackexchange.com/a/226037/47547 is basically what I'm doing, but it doesn't go far enough, since in your suggestion the request_path is just replaced, I want to add or subtract request paths.
    – Steve
    May 10, 2022 at 16:45
  • 1
    Can you just use implode/explode to convert the string to array and then back after you make adjustments?
    – sonfd
    May 10, 2022 at 17:01

1 Answer 1

0

But, the current block visibility request path variable is a string, containing paths separated by commas.

This is the summary display, not the data format saved in config. Here the raw input data is saved along with the new lines entered by the user.

If you want to follow @sonfd comment you can rewrite the code of the linked answer:

$block = \Drupal\block\Entity\Block::load('openidconnectlogin');
$visibility = $block->getVisibility();
if (isset($visibility['request_path']['pages'])) {
  $pages = $visibility['request_path']['pages'];
  $paths = array_map('trim', explode("\n", $pages));

and later

  $pages = implode("\n", $paths);
  $visibility['request_path']['pages'] = $pages;
  $block->setVisibilityConfig('request_path', $visibility['request_path']);
  $block->save();
}
2
  • Where are you getting the $pages array?
    – Steve
    May 10, 2022 at 18:46
  • $paths is the array (where you make the adjustments). To make it clear I combine both, the code from the previous answer and the new one.
    – 4uk4
    May 10, 2022 at 19:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.