Get Attribute Values
Permalink 1 user found helpful
Could someone enlighten me on how to get all the attribute values by an attribute handle?
I want to populate a form select field with all the different attribute values for a particular attribute.
Many, many thanks!
I want to populate a form select field with all the different attribute values for a particular attribute.
Many, many thanks!
This should do it. Place this in a template page, or a block.
I need to do this from a block controller or block add/edit form. Any tips there?
To clarify: I need all possible attribute values to populate an array.
To clarify: I need all possible attribute values to populate an array.
Are you talking about AttributeKeys or AttributeValues?
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?
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?
Yes,
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.
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.
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.
Took a quick in the database, and it seems all your values will reside in CollectionSearchIndexAttributes
so... i would do something like the following :
the field name will be the handle for the attribute with 'ak_' prepended to it
JD
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
This works, but if you allow multiple selected values in your select attributes, then you'll need to parse the database strings as follows:
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);
As an update to the previous post, the foreach loop can be replaced by the array_merge function. Cleaner code, and probably more efficient.
$myValues = array_merge($myValues, $valueArray);
Is there a way to modify this code to be used within a page template? I have a custom attribute that contains multiple values, I'd like to display it as a list within the actual template and not in a controller. Any ideas? I appreciate the help!
FAI (For Anyone's Information):
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.
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.
If anyone is still interested, I figured out how to get the attributes using this code:
In my case I add a "Select" type attribute called "categories".
In my case I add a "Select" type attribute called "categories".
This may be a bit late on the draw here, but someone might find this useful...
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...
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.
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.
Searching..
This works, but it feels very wrong to have to go through so many steps to get the attribute values!
I agree, but it's the best I could figure out using the Concrete5 API. Probably the best thing to do would be to create a utility class that directly accesses the database. I'm surprised that Concrete5 hasn't implemented something.
$at_cat = AttributeKeyCategory::getByHandle('collection');
$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
$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