Setting page attribute on block add/edit

Permalink
Hello there,

I'm beggining with concrete5 and I've stumbbled on a bit of a problem.

When adding/editing a block the block's controller "save" function is called, all ok.

- dl_entry_title is a custom attribute (text)
- dl_entry_image_id is a custom attribute (number)

public function save($args) {
        $args['field_2_image_fID'] = empty($args['field_2_image_fID']) ? 0 : $args['field_2_image_fID'];
        $this->setAttributesOnPage($args['field_1_textbox_text'], $args['field_2_image_fID']);
        parent::save($args);
}
private function setAttributesOnPage($title, $imageId)
    {
        $attribTitle = CollectionAttributeKey::getByHandle('dl_entry_title');
        $attribImageId = CollectionAttributeKey::getByHandle('dl_entry_image_id');
        $currentPage = Page::getCurrentPage();
        //$currentPage->clearAttribute($attribTitle);
        //$currentPage->clearAttribute($attribImageId);
        $currentPage->setAttribute($attribTitle, $title);
        $currentPage->setAttribute($attribImageId, $imageId);
    }


This code will randomly work on setting the attributes.
Am I doing something wrong?
Thank you for your time.

View Replies: View Best Answer
JohntheFish replied on at Permalink Reply
JohntheFish
Do you plan on having more than 1 of this block on a page? If so, they will be arguing with each other over the same page attributes.
livenfly replied on at Permalink Reply
The page will only have one block:

$a = new Area('Main');
$a->setBlockLimit(1);
$a->display($c);


Thanks.
JohntheFish replied on at Permalink Reply
JohntheFish
In that case your code looks OK. Though you don't need to get the attribute key objects first - setAttribute should accept an attribute handle directly. However, that is unlikely to be the issue.

As you are developing, I am guessing that cacheing is turned off to eliminate any cache issues.

Is it totally random, or is it under particular chains of circumstances, such as adding a block to a new page before the page has been saved for the first time?
livenfly replied on at Permalink Reply
"As you are developing, I am guessing that cacheing is turned off to eliminate any cache issues."

Yes, I turned off the cache in "Cache & Speed Settings".
I've been debugging the php code and I added a line:

$a1 = $currentPage->getAttribute('dl_entry_title');
//$currentPage->clearAttribute($attribTitle);
//$currentPage->clearAttribute($attribImageId);
$currentPage->setAttribute($attribTitle, $title);
$currentPage->setAttribute($attribImageId, $imageId);


in order to read before writing the new value. As I keep making changes to the block the value "$a1" remains the same, and it's not updating.

"Is it totally random, or is it under particular chains of circumstances, such as adding a block to a new page before the page has been saved for the first time?"

Just before I wrote this reply I tried to change the block fields and the attributes where updated, when i tried again it failed to update... I double checked the cache settings for the page and got:

"The cache has been disabled. Full page caching is not available."

And the overall cache settings are:

Basic Cache
Off - Good for development.
Overrides Cache
Off - Good for development.
Full Page Caching
Off - Turn it on by hand for specific pages.
Full Page Cache Rebuild
Automatic.
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
I cant offer any more ideas.

I have a free block in the marketplace 'quick attribute view' which may help provide diagnostics. Though I am not sure it would give any added value in this situation.

See
http://www.concrete5.org/marketplace/addons/quick-attribute-view/...
livenfly replied on at Permalink Reply
Man, I'm really sorry, suddenly it started working... Maybe I missed something. I'm changing the block controller code while shift+F5 to reload things from scratch, maybe the file wasn't updated on the site, I don't know.
Either way thank you for your time. I'm marking your last answer as correct.

Edit:
//$currentPage = Page::getCurrentPage();
$currentPage = $this->getCollectionObject();


This line removed the erratic behaviour.
jegra replied on at Permalink Reply
FYI - I was seeing similar erratic behavior with saving page attribute values from within a block. The approach you suggested:
$currentPage = $this->getCollectionObject();

worked for me as well. Many thanks!
michaelDotco replied on at Permalink Reply
I am having this problem too. Here is the code

if ($args['Live'] == '1'):
$switchAll = '0';
else: $switchAll = '1';
endif;

Page::getCurrentPage()->setAttribute('isLive', $args['Live']);
Page::getCurrentPage()->setAttribute('exclude_nav', $switchAll);
Page::getCurrentPage()->setAttribute('exclude_page_list', $switchAll);
Page::getCurrentPage()->setAttribute('exclude_search_index', $switchAll);
Page::getCurrentPage()->setAttribute('exclude_sitemapxml', $switchAll);
parent::save($args);


When you go to the page from the sitemap it wont update the first time you edit the block. Do it again and it will.
michaelDotco replied on at Permalink Reply
Changed to this and it seems to work

if (!empty($args['Live'])):
$switchAll = '0';
else: $switchAll = '1 ';
endif;

Page::getCurrentPage()->setAttribute('isLive', $args['Live']);
Page::getCurrentPage()->setAttribute('exclude_nav', $switchAll);
Page::getCurrentPage()->setAttribute('exclude_page_list', $switchAll);
Page::getCurrentPage()->setAttribute('exclude_search_index', $switchAll);
Page::getCurrentPage()->setAttribute('exclude_sitemapxml', $switchAll);
parent::save($args);
}