Get Attribute Values
Permalink 1 user found helpfulI want to populate a form select field with all the different attribute values for a particular attribute.
Many, many thanks!

To clarify: I need all possible attribute values to populate an array.
It sounds like you want to get all the AttributeValues of all an AttributeKey associated to all the Collections in a list. Is that correct?
I want to get all attribute values for the attribute "directory_groups" and put each value as a separate item in an array.
So then I can take that array and use it as the options for a select field.
<?php $groupOptions = // Magic here echo $form->select('dirGroup', $groupOptions, $dirGroup);
I either need to get the array of values from a block controller or add/edit form of a block.
I want the select field to be displayed in a custom block that I'm creating that will allow a user to select a single "directory group" from a list of many options.
so... i would do something like the following :
$db = Loader::db(); $r = $db->query("SELECT distinct ak_my_custom_attribute FROM CollectionSearchIndexAttributes"); $myValues = array(); while ($row = $r->fetchRow()) { $myValues[] = ($row['ak_my_custom_attribute']); }
the field name will be the handle for the attribute with 'ak_' prepended to it
JD
while ($row = $r->fetchRow()) { //search for multiple values $valueArray = explode("\n",$row['ak_department']); foreach ($valueArray as $value) { $myValues[] = $value; } } $myValues = array_unique($myValues); sort($myValues);
$myValues = array_merge($myValues, $valueArray);
This advice seems painfully off to me(?). Any table that ends with "*searchIndexAttributes" is not the place to go to access/retrieve attributes. Those values have been stuck in there by the C5 core engine to help with running filters against them; hence the "ak_" pre-pending (and so forth). Any attribute category (custom or core) that wants to have the attributes which are bound to it be searched will have the method "getIndexSearchTable" in it, which c5 will use to create the tables mentioned in this thread. Any attribute type that wishes to be searched will need to have a method within it's controller called searchKeywords with the above mentioned syntax. It's in the documentation (barely). :)
Accessing attribute values depends on where you placed them. Is it a page attribute, a user attribute, a custom attribute category??? Having the "keys" is only half the battle. Now you've got to figure out where you parked the car.
In my case I add a "Select" type attribute called "categories".
If you need the possible values for an attribute, but don't want to call the database directly and don't want to reference the Attribute Key by it's ID, the following will work...
$terms = array(); $set = AttributeSet::getByHandle('some_set'); $keys = $set->getAttributeKeys(); foreach($keys as $key) { $handle = $key->getAttributeKeyHandle(); if( $handle == 'some_attribute' ) { $type = $key->getAttributeType(); $cont = $type->getController(); $cont->setAttributeKey($key); $terms = $cont->getOptions(); break; } }
The drawback to this approach is that you must have the Attribute added to an Attribute Set, and then loop through that set to get the AttributeKey object. This isn't a big deal if you add the Attribute to an Attribute Set with only the single Attribute added. However, it's still not ideal.
If anyone knows of a better way to get an AttributeKey by it's Handle, I'd would be interested in knowing. Not sure why the good people of C5 don't have a static getByHandle method in the class.
Additionally, I know this works for the Select Attribute Type, but have not tested on any other types. It's very possible that the getOptions method is specific to the Select Attribute Type.
$at_key = $at_cat->getAttributeKeyByHandle('your_at_key_handle');
$at_key_id = $at_key->getAttributeKeyID();
$at_key->render('form');
$at_key->outputSearchHTML(); //similar to render