Score:0

How can I programmatically add or remove a new request_path to block visibility?

in flag

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.

4uk4 avatar
cn flag
Is this the answer to your question? https://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.
cn flag
@4uk4 it’s open now (your reopen vote is binding because of your score in the related tags)
in flag
@4uk4 your answer at https://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.
sonfd avatar
in flag
Can you just use implode/explode to convert the string to array and then back after you make adjustments?
Score:0
cn flag

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();
}
in flag
Where are you getting the `$pages` array?
4uk4 avatar
cn flag
$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.
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.