Forum

Thread tagged as: Question

How do I test if a region has been saved?

What I'm trying to do

I'm trying to test if a region has either a blank value or has not yet been saved. If it's blank/has not been processed then I want to set a default value.

I thought this would be straight forward, however for some reason if the region has not been saved at all Perch seems to return an empty string with 38(?) characters. If the region has not yet been saved and I do a PHP variable dump using var_dump($album_name_override); it returns string(38) "". I've even tried to trim this using trim($album_name_override); but it returns the same result.

Here is a simple example of what I'm trying to do:

/* [1] Create an Album name region */
perch_content_create('Album Name', array(
    'template' => '_gallery_album',
));

/* [2] Test if the album region exists */
/* Get the Album Name region value. */
$album_name_override = perch_content('Album Name', true);

/* Test its value */
if ($album_name_override !== '') {
    /* If the "Override" region has an override value, set the album name to this <-- THIS ALSO SEEMS TO GET TRIGGERED IF THE REGION HAS NOT YET BEEN SAVED */
    $album_name = $album_name_override;
} else {
    /* If the "Override" region has not been saved, or exists with a blank value */
    $album_name = $default;
}

I'd be grateful for any pointers.

Jay George

Jay George 2 points

  • 4 years ago

I finally figured out what's being returned to $album_name_override is "<!-- Undefined content: Album Name -->", which is where the 38 characters is coming from.

To take this into account the code should instead be:

if (stripos($album_name_override, 'undefined content') !== false) {
    /* If the "Override" region has not yet been saved */
    $album_name = $default;
} else if ($album_name_override !== '') {
    /* If the "Override" region has an override value */
    $album_name = $album_name_override;
} else {
    /* If the "Override" region exists with a blank value */
    $album_name = $default;
}
Drew McLellan

Drew McLellan 2638 points
Perch Support

That's right. Or use perch_content_custom(), which is more designed for the programatic retrieval of content.