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 :
$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?

 
kino replied on at Permalink Reply
kino
Do you tried using getDisplayValue?
hallucinant replied on at Permalink Reply
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;
}
andrew replied on at Permalink Reply
andrew
Can you try getOptions() instead?
hallucinant replied on at Permalink Reply
That's what I'm trying, but can't seem to figure out how it's been called.

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?
andrew replied on at Permalink Best Answer Reply
andrew
Ah, got it. Try something like this:

$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.
hallucinant replied on at Permalink Reply
sweet, Thanks Andrew!
TheRealSean replied on at Permalink Reply
TheRealSean
Thank you just what I was after, #HelpfulThread
aryeh replied on at Permalink Reply
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?
TheRealSean replied on at Permalink Reply
TheRealSean
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
aryeh replied on at Permalink Reply
your right the attribute was not called city,
sorry my mistake .

thanks a lot. Aryeh