How to get content of block into Attribute

Permalink 2 users found helpful
Hi,
I need to get content of specific block into Attribute.

I'm trying to do this:
Concrete/Blocks/Myblock/View.php:
<form name="test">
<input type="hidden" value=" <?php echo $keyword1.", ".$keyword2.", ".$keyword3; ?> " name="page">
</form>


Concrete/Theme/Mytheme/default.php:
<?php 
Loader::model('collection_attributes'); 
$c->setAttribute('meta_keywords', $page);
?>


But it doesen't work. I recevied this bug:
Catchable fatal error: Object of class Page could not be converted to string in /home/www/restaurantcorsini.cz/subdomeny/www/indexjan/concrete/libraries/3rdparty/adodb/adodb-lib.inc.php on line 150

Or if You have some better ideas how to get content of block inside attribute, let me know. I appreciate all the help.

Thanks!

 
pvernaglia replied on at Permalink Reply
pvernaglia
You need to process the form in the blocks controller.php with a function with the same name as the forms action. The form in view.php needs an action assigned to it;

<form method="post" action="<?php echo $this->action('test'); ?>">
Then in your controller you can set the attribute with the post data from the form
function action_test() {
  Loader::model('collection_attributes'); 
  $c->setAttribute('meta_keywords', $this->post('page'); 
  return;
}


If you search around the forums you can find more detailed examples
northon replied on at Permalink Reply
Unfortunately, Your solution is not working, too. I don't know why :( What's wrong?
beebs93 replied on at Permalink Reply
beebs93
Sorry, I'm a little confused. Is this going to be a form that anyone can submit and whatever they input will be set as to a specific attribute for the current page? Could you give more info on how this will be used?
pvernaglia replied on at Permalink Reply
pvernaglia
I thought you were asking about forms, you had the beginnings of one in your question. I really don't know what you want to do.
northon replied on at Permalink Reply
No, at first You need to login. Then You add block and his content will be value for the attribute. In this attribute, every page will have their specific value which depends on what you type to the block on this page.
jordanlev replied on at Permalink Best Answer Reply
jordanlev
First of all, don't put your block inside the concrete directory -- put it in your site's top-level "blocks" directory instead.

If I'm understanding what you want to achieve, you need to process the form in the block controller and then set the attribute from there. I'm not sure this will even work as you want it to, though, because it's possible that setting the attribute in that way happens too late for the view to pick it up. Also, it sounds like you want this attribute to only be set for that one page view, but I think setting a page attribute in the way you want will set the attribute to that in a more permanent way (until it's set again by someone else using the form). If you do only want the "attribute" set for one page view, don't use an attribute at all -- instead just pass the value to the view from the controller function that processes the form.

Here's how you put the form in the block view:
<form method="post" action="<?php echo $this->action('submit_form'); ?>">
  <input type="hidden" name="keyword1" value="<?php echo $keyword1; ?>
  <input type="hidden" name="keyword2" value="<?php echo $keyword2; ?>
  <input type="hidden" name="keyword3" value="<?php echo $keyword3; ?>
</form>

Note that I've put each of your keywords into a separate input tag, because otherwise it will be annoying to parse out the individual values.

Then in your block's controller.php file, add this function:
function action_submit_form() {
  //First, retrieve the submitted form values:
  $keyword1 = $this->post('keyword1');
  $keyword2 = $this->post('keyword1');
  $keyword3 = $this->post('keyword1');
  //To just pass the values back to the view so you can use them one time, do this:
  $this->set('keyword1', $keyword1);
  $this->set('keyword2', $keyword2);
  $this->set('keyword3', $keyword3);
  //Or if you want to actually set the page attribute, do this:
  $page = Page::getCurrentPage();
  $page->setAttribute(CollectionAttributeKey::getByHandle('your_attribute_handle1'), $keyword1); //<-- change the handle and the value to what you want.
  $page->setAttribute(CollectionAttributeKey::getByHandle('your_attribute_handle2'), $keyword2); //<-- change the handle and the value to what you want.
  $page->setAttribute(CollectionAttributeKey::getByHandle('your_attribute_handle3'), $keyword3); //<-- change the handle and the value to what you want.
}


Hope that helps.

-Jordan
ScottC replied on at Permalink Reply
ScottC
jordan is absolutely right, just make sure that you are aware that when you call setAttribute you aren't setting a runtime attribute, this attribute is a true collection attribute that persists between requests and might override any existing attributes you already have.

I ran across this once cowboy coding something and it ended up not being what someone wanted, just putting that out there for you and anyone else that reads this later on.
northon replied on at Permalink Reply
Yes, that is what I exactly want to do. Than I call it in my default.php (using
<?php echo $c->getCollectionAttributeValue('klicova_slova') ?>
) but I don't see anything, attribut wasn't set. So, maybe is necessary to set the value earlier. Have you any ideas?
northon replied on at Permalink Reply
Yeah, I've made some changes and what's happened, it's working now!!

Here's the code:

view.php:
Loader::model('collection_attributes'); 
$page = Page::getCurrentPage();
$page->setAttribute('meta_keywords', $keyword1);
?>
<form method="post" action="<?php echo $this->action('submit_form'); ?>">
  <input type="hidden" name="keyword1" value="<?php echo $keyword1; ?>">
  <input type="hidden" name="keyword2" value="<?php echo $keyword2; ?>">
  <input type="hidden" name="keyword3" value="<?php echo $keyword3; ?>">
</form>


controller.php:
Loader::model('collection_attributes'); 
function action_submit_form() {
  //First, retrieve the submitted form values:
  $keyword1 = $this->post('keyword1');
  $keyword2 = $this->post('keyword1');
  $keyword3 = $this->post('keyword1');
  //To just pass the values back to the view so you can use them one time, do this:
  $this->set('keyword1', $keyword1);
  $this->set('keyword2', $keyword2);
  $this->set('keyword3', $keyword3);
}


Now, I just need to assign $keyword1,$keyword2,$keyword3 to one attribute.
I hope that I can do it this way:
$page->setAttribute('meta_keywords', $keyword1.", ".$keyword2.", ".$keyword3);


I thank You all for reply, solved.
northon replied on at Permalink Reply 1 Attachment
For those who want use this block, I upload this block in attachment. I gave there notes, to explain what and where you can change..