Joss Posted November 9, 2014 Share Posted November 9, 2014 UPDATE: I have now changed the title of the post because the situation got more confused and the problem worked out differently. The problem has not been solved, and there is a better breakdown of the issue here: https://processwire.com/talk/topic/8231-check-if-page-field-is-set/?p=80520 ###################### Original post: I have no idea what is wrong with my brain today. I am using a page field to generate a link in a block. If a page is not selected, I dont want to show the linking markup. So, how do I check whether it has been set or not? Just doing if($block->page_field) throws an internal server error. if(isset doesn't seem to do anything. nor does if(empty so... ? Help! EDIT: This is not in a loop, by the way Link to comment Share on other sites More sharing options...
diogo Posted November 9, 2014 Share Posted November 9, 2014 single or multiple? Link to comment Share on other sites More sharing options...
interrobang Posted November 9, 2014 Share Posted November 9, 2014 It depends on how you configured you page_field on the Details Tab. For single page references this should work always. First test if page_field is "trueish", then check if not a NullPage if($block->page_field && $block->page_field->id){} for multi page references a simple count should work: if(count($block->page_field)){} Link to comment Share on other sites More sharing options...
Joss Posted November 9, 2014 Author Share Posted November 9, 2014 Single (I am not a morman) Link to comment Share on other sites More sharing options...
Joss Posted November 9, 2014 Author Share Posted November 9, 2014 @interrobang Nope. This is how the entire thing works. Have a page field used for selecting a single block That is fine. The block template has a page field for selecting a link to a particular other page. I want it so that if a link is selected then a nice button shows. But if it isn't, nothing shows. The same code works fine inside a foreach loop for choosing multiple blocks, but fails when I use it to select just a single block with a single select. Here is the full code: function singleBlockServiceModels(){ $block = wire("pages")->get("/servicing/")->block_single_select_models; $color = $block->block_bgcolour; $rgba = hex2rgba($color, 0.5); $thisanchor = "#{$block->block_anchor}"; $out =""; $out .="<div class='columns block-container sinlge-block' style='background-color:{$rgba};' data-equalizer-watch>"; if($block->block_show_headline){ $out .="<div class='block-header'>"; $out .="<h4 class='text-center'>{$block->block_headline}</h4>"; $out .="</div>"; } if($block->block_image){ $out .="<div class='block-image'>"; $out .="<img src='{$block->block_image->url}' alt='{$block->block_image->description}'>"; $out .="</div>"; } $out .="<div class='block-body'>"; $out .="{$block->block_text}"; $out .="</div>"; if($block->block_link->url){ $out .="<div class='block-link text-center'>"; $out .="<a href='{$block->block_link->url}{$thisanchor}' class='readmore-button '>Find Out More</a>"; $out .="</div>"; } $out .="</div>"; // column echo $out; } Oh, the rest of it works if I remove the if statement at line 23 onwards Link to comment Share on other sites More sharing options...
diogo Posted November 9, 2014 Share Posted November 9, 2014 "Single page (Page) or boolean false when none selected" or "Single page (Page) or empty page (NullPage) when none selected"? Try to make a joke with this Link to comment Share on other sites More sharing options...
Joss Posted November 9, 2014 Author Share Posted November 9, 2014 er ,,,,,, Its a Null Page single (well, you only get a page at a wedding so if you are single you haven't got one in the first place) I have tried with both. Mind you, haven't tried isset with boolean false... yet. Link to comment Share on other sites More sharing options...
interrobang Posted November 9, 2014 Share Posted November 9, 2014 I don't see why my suggested check should not work. Did you try it? I guess you are talking about the field block_link? if($block->block_link && $block->block_link->id) { $out .="<div class='block-link text-center'>"; $out .="<a href='{$block->block_link->url}{$thisanchor}' class='readmore-button '>Find Out More</a>"; $out .="</div>"; } EDIT: if you know it is a "Null Page Single" only shecking for id should be enough if($block->block_link->id){} Link to comment Share on other sites More sharing options...
Joss Posted November 9, 2014 Author Share Posted November 9, 2014 No, still gives me a 500 Link to comment Share on other sites More sharing options...
Joss Posted November 9, 2014 Author Share Posted November 9, 2014 This also does not work: if(!($block->block_link instanceof NullPage) ){ } Link to comment Share on other sites More sharing options...
diogo Posted November 9, 2014 Share Posted November 9, 2014 That's strange, we may need Florian's help with this. Link to comment Share on other sites More sharing options...
Joss Posted November 9, 2014 Author Share Posted November 9, 2014 ha! Link to comment Share on other sites More sharing options...
Joss Posted November 9, 2014 Author Share Posted November 9, 2014 Okay, it looks like I am all backwards on this. Sorry, tried so many variations I am getting lost. It appears to go wrong when there IS a link chose. Actually, there seems to be some problem with $block->block_link->url all on its own. Link to comment Share on other sites More sharing options...
Joss Posted November 9, 2014 Author Share Posted November 9, 2014 SOLVED IT - er, I think The problem is that if the block link is pointing to the same page as the block is being displayed on, then it fails - which is why I was getting a 500. So, I am going to have to work out a check here of some sort to see if $block->block_link->name is the same as the last bit of the url of the page. grief! Link to comment Share on other sites More sharing options...
diogo Posted November 9, 2014 Share Posted November 9, 2014 Shouldn't happen... Link to comment Share on other sites More sharing options...
Joss Posted November 9, 2014 Author Share Posted November 9, 2014 Well, I changed the link and it all went away... So, I dont know. Link to comment Share on other sites More sharing options...
Joss Posted November 9, 2014 Author Share Posted November 9, 2014 Actually, it is even more odd than that. I cant link to anything in the same branch of the tree. However, the other group of block, which are a multiselect field and therefore are within a foreach, have no problems Link to comment Share on other sites More sharing options...
diogo Posted November 9, 2014 Share Posted November 9, 2014 Can you create a new field to see if it works? Link to comment Share on other sites More sharing options...
Joss Posted November 9, 2014 Author Share Posted November 9, 2014 Okay, I have found a solution. If I make it a multi field, but use just a single select drop down and then change my Get to include first() it works. So, as long as its part of a wirearray I am okay. Makes me want to shudder! Can you create a new field to see if it works? I had the same problem with another version of the field that is in a different part of the system - it did the same thing, but was set for a different list. Link to comment Share on other sites More sharing options...
Joss Posted November 9, 2014 Author Share Posted November 9, 2014 Okay, just add complication... On some pages I have a select that just selects a block, so: $block = wire("page")->block_single_select; Those dont have a problem. On other pages, they are getting their blocks from a central setting: $block = wire("pages")->get("/servicing/")->block_single_select_manufacturers; Those fail unless I turn them into multi and call them like: $block = wire("pages")->get("/servicing/")->block_single_select_manufacturers->first(); That works. ########### part of the problem here is this was meant to be a very simple site and has become very complicated. I will write a case study called: "How to over complicate a solution and end up building a site that is worth far more than the client will actually be paying" Link to comment Share on other sites More sharing options...
Joss Posted November 17, 2014 Author Share Posted November 17, 2014 Just to finish this up - there definitely is a problem, even though I have worked around it for the moment. So here is a careful breakdown of what is going on in case Ryan has the chance to look at it on his way past. Note: please ignore earlier posts in this thread - this is the definitive explanation! Part 1 In a template I have a page field that selects a block from a list of pages stored under a hidden parent away from the main tree. Within the template for the block there is a page field used to select a page to create a link to that page via a button, This is a single select, obviously. I am using a page field here so I can limit what page can be selected. When I create a block, it has an image, some text, a headline and optionally a link using the page field. (if no page is selected, then the button is not shown) Part 2 The blocks are used in two different ways in the most pages on the site: 1. In a set of three using a multi-select and asm. These appear in a nice little line - how cute. 2. As a single select in a different part of the page. The multi select generates no problems at all. Part 3 With the single select, everything is fine until, within the block, I choose a page link (using the page field) that is within the same part of the tree as the page within which the block will be displayed. To make it clear, here are two parts of the site: servicing --- Little Service --- Big Service repairs --- brakes --- exhausts (Note the site is actually more complicated than that, but I don't want to bore you!) Lets say that we create a block about brakes that includes a link (using the page field in the block template) that goes to the brakes page. On the Little Service page, I now select that block using a the single select page field. The block displays fine. On the exhausts page, I select the block using the single select page field It throws a 500 error. And that is it. My current work around is that I have made the single select field into a multi with ASM and put a note that only the top most block will be used. I have used first() to retrieve the block in the template. That does not break it. I have tried both types of single select by the way. I haven't a clue what is going on! Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now