List all options from a form (select, checkboxes)
Permalink 1 user found helpful
Hi,
Everything seems to be linked to a Collection (CollectionAttributeKey for instance), and simply getting all the values from a custom attribute is proving to be challenging.
Is there a method that would list the options of an attribute, which I could manipulate?
This is the only way I found so far :
You can then render your object. Thing is, if nothing is selected, it returns the boolean false (because the collection doesn't have it).
I would like to alter it in some way, not pass it directly to the form Helper. Even better, only have an object w/ all the options available.
Any ideas?
Everything seems to be linked to a Collection (CollectionAttributeKey for instance), and simply getting all the values from a custom attribute is proving to be challenging.
Is there a method that would list the options of an attribute, which I could manipulate?
This is the only way I found so far :
$cAttributes = CollectionAttributeKey::getList(); foreach($cAttributes as $key => $attrValue) { if($attrValue->getAttributeKeyHandle() == 'my_attribute') { $myObject = $c->getAttributeValueObject($attrValue); } } $attrValue->render('form', $myObject);
You can then render your object. Thing is, if nothing is selected, it returns the boolean false (because the collection doesn't have it).
I would like to alter it in some way, not pass it directly to the form Helper. Even better, only have an object w/ all the options available.
Any ideas?
Do you tried using getDisplayValue?
Well, as you can see, it would only return the selected options, not all the options, so that won't do, thanks though
public function getDisplayValue() { $list = $this->getSelectedOptions(); $html = ''; foreach($list as $l) { $html .= $l . '<br/>'; } return $html; }
Can you try getOptions() instead?
That's what I'm trying, but can't seem to figure out how it's been called.
At this point I know getOptions() is being called, by the render() method. I'd like to call it *before*.
How do you instantiate that SelectAttributeTypeController class w/o calling the collection?
Loader::model('attribute/types/select/controller'); $attrValue->render('form', $myObject);
At this point I know getOptions() is being called, by the render() method. I'd like to call it *before*.
How do you instantiate that SelectAttributeTypeController class w/o calling the collection?
Ah, got it. Try something like this:
that will give you an array named $opts where the keys are all the select attribute option IDs, and the values are the text values.
$ak = CollectionAttributeKey::getByHandle('your_attribute_handle'); $akc = $ak->getController(); $options = $akc->getOptions(); $opts = array(); foreach($options as $opt) { $opts[$opt->getSelectAttributeOptionID()] = $opt->getSelectAttributeOptionValue(); }
that will give you an array named $opts where the keys are all the select attribute option IDs, and the values are the text values.
sweet, Thanks Andrew!
Thank you just what I was after, #HelpfulThread
i put the following code on a page type:
<?php
$ak = CollectionAttributeKey::getByHandle('city');
$akc = $ak->getController();
$options = $akc->getOptions();
$opts = array();
foreach($options as $opt) {
$opts[$opt->getSelectAttributeOptionID()] = $opt->getSelectAttributeOptionValue();
}
?>
i get error: Fatal error: Call to a member function getController() on a non-object in /home/yossi/public_html/themes/real_estate_template/properties.php on line 44
line 44: $akc = $ak->getController();
does anyone know what my problem is ? , do i need to include some controller or something?
<?php
$ak = CollectionAttributeKey::getByHandle('city');
$akc = $ak->getController();
$options = $akc->getOptions();
$opts = array();
foreach($options as $opt) {
$opts[$opt->getSelectAttributeOptionID()] = $opt->getSelectAttributeOptionValue();
}
?>
i get error: Fatal error: Call to a member function getController() on a non-object in /home/yossi/public_html/themes/real_estate_template/properties.php on line 44
line 44: $akc = $ak->getController();
does anyone know what my problem is ? , do i need to include some controller or something?
It suggests that it was not able to get the attribute, is the attributes handle 'city'? and is the attribute a page attribute? not a file/user
your right the attribute was not called city,
sorry my mistake .
thanks a lot. Aryeh
sorry my mistake .
thanks a lot. Aryeh