EDIT: I changed this issue based on post #6.
Before adding a PHP filter to a block, the text (status?) under PHP in the left vertical tab said "Not restricted". After adding the PHP code (which returns either TRUE or FALSE), that text said "Restricted to certain pages".
I ended up not needing the PHP code to restrict that block, so I edited the block and removed all the text in the PHP filter field. After saving, the block was not showing as it should. I edited the block again and saw the text under PHP still showed "Restricted to certain pages", even though the text field was empty.
The block in question is a Views block, so I deleted it from the Block Layout page and added it again using the "Place block" button. The recreated block showed "Not Restricted" for the PHP filter and the block was showing as expected.
| Comment | File | Size | Author |
|---|---|---|---|
| #13 | php_condition_check_empty-2678430-13.patch | 500 bytes | anybody |
Comments
Comment #2
hass commentedBy default you need set return TRUE or it defaults to FALSE
Comment #3
extexan commentedBut I just wanted to "undo" it... that is not set it - not use it. But emptying the field didn't make it go back to the default condition of "Not restricted". Only by deleting and re-adding it was I able to clear it.
Comment #4
extexan commentedPerhaps I should offer the steps to reproduce. I'm not sure if this is only on Views blocks, and I don't want to try it on other blocks in my dev site. So my steps to reproduce will assume it is Views blocks-related.
Comment #5
extexan commentedComment #6
extexan commentedAhhh, I see now. The PHP code I blanked out (mentioned in the original post) was added by another dev working on the same project, so I never saw what was there by default - which is:
That's what you meant by your answer. However, I think having that PHP code being parsed and executed for every block on every page load is unnecessary overhead.
Much better... if the PHP Filter field is empty, have the PHP Filter module return TRUE.
I'm changing the title (and other details) of this issue to reflect this.
Comment #7
extexan commentedComment #8
extexan commentedComment #9
anybodyThis is ABSOLUTELY critical!
The module should definitely behave like the title describes and a new version should be released. I wasted 4 hours searching for the reason why many of my blocks were not showing up whatever I did.... :(
The reason was the empty php block visibility field...
Comment #10
hass commentedUse the latest release.
Comment #11
anybodyHello hass,
thank you very much for your quick reply.
Well I did but it didn't work for already existing blocks. Furthermore I can't see any related code fixing this. Or have you just created a new release?
This issue also doesn't contain a commit message. I'm sorry if the mistake is on my side.
Comment #12
anybodyPS: I'll retry the dev version and provide feedback to make sure I'm not wrong.
Comment #13
anybodyNo it definitely doesn't work. Patch attached.
Comment #15
hass commentedhttp://cgit.drupalcode.org/php/commit/?id=0018afb
Comment #16
jeff veit commentedI've tried this and I think it still needs work.
The default in D7 is to return TRUE unless the PHP filter has code which returns FALSE for access allowed.
This means that all the following show the block on all pages (inverted commas excluded):
In D8, if you do not have
<?php Return TRUE; ?>as the field, then the block does not display. For upgrades from D7, this means that every PHP filter has to be checked. (The Security Review module will tell you which nodes have PHP filters, but it didn't work for me in D8, and I'm not certain that it tells you about blocks. It would be useful to have a report of which content has the filter without Security Review, wouldn't it?.)The patch above works as long as the PHP filter is empty. [Edit: by empty I mean unset.] But if it's had anything in it [Edit: I mean set, and it could even be set to ''], then this:
causes the problem because when the block is instantiated because
And bam! that value for the php field is used instead of the default from the patch.
I've only thought of one possible solution, and it's not elegant... Have two fields instead of one for the php field. One which contains a raw value (i.e. the current php field, but renamed - say php_raw), and one which is dynamically created if php_raw contains the string '<?php' - the new field would be called 'php' so that the existing code all continues to work.
Maybe there are other ideas? Or do all upgrades from D6 or D7 not work properly?
[Edited for clarity.]
Comment #17
jeff veit commentedComment #18
hass commentedWell the current approach is not perfect, but it works as it should / designed. I thought about rebuilding the old radio boxes on top of the code area. These has enabled/disabled the PHP filter in past. At least it makes the configuration easier, but it would also not solve your issue.
But what is clearly wrong and must break is random text, double/single quotes. This is not PHP code. You had some luck that is worked in past. And as always with wonkey implementation they may break in future.
I'm open for usability enhancements.
Comment #19
jeff veit commentedI sympathise with random text, but I think it's a bug if it's empty - i.e. '' or ' ' (or even any number of spaces, tabs, returns - blank text) and that causes the block not to appear, since it changes the semantics from D7. Especially if D6 upgrades (as is the case when I found this) cause the problem.
Comment #20
alexrayu commentedThis is not a support request. This is a major logic bug. Blocks disappear after this module is installed.
Comment #21
ao2 commentedThis is a major issue indeed.
As of now, the Php Condition plugin forces every newly configured block to hide by default, I experienced this after adding blocks created via the “Custom block library” UI. They would not show up because of this bug.
And any block is also hidden by just clicking “Configure“ in the “Block layout” page and then “Save block” without even changing anything in their configuration.
The commit mentioned in #15 is a step in the right direction but it's not enough as explained by Jeff Veit in #16.
The patch in #13 would work but it's incomplete because the javascript side of the UI expects the string
'<?php return TRUE; ?>'to set the'Not restricted'label.Given the analysis from #16 I'd say that the proper way to fix this could be to override
setConfiguration, like I am proposing in the attached patch.Comment #22
ao2 commentedMmh, the latest version of the module
8.x-1.0-beta2with the commit from #13 does seem to work indeed.The problem here was that
composer require drupal/phpwas by default pulling in8.x-1.0-beta1instead, I am not sure why, and that version is severely bugged.So the patch I proposed in #21 is not strictly necessary if you create the blocks after installing the latest version because the default configuration will be set to
<?php return TRUE; ?>and everything will just work. Can someone else confirm this?I am hiding my patch and showing again the previous one which can help if some blocks were created with an old version of the module.
A long term solution to avoid this problem with the form content could be to validate it when saving it, making sure that the code is inside the php tags, this way the user could never set an empty value.