Setting page attribute on block add/edit
PermalinkI'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.

$a = new Area('Main'); $a->setBlockLimit(1); $a->display($c);
Thanks.
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?
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.
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/...
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.
$currentPage = $this->getCollectionObject();
worked for me as well. Many thanks!
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.
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);
}