Fetching attribute set data from another page

Permalink
Long time listener, first time caller...

Project I'm working on; I have a bunch of product pages on a site. I'm using some custom page attributes to set various specs about the products. Industries they're used in, applications they're used for, and so on.
So I have an 'Industries' set, an 'Applications' set, etc...

Now, the problem...
I need to be able to display this information on an overview page, split by set by product.
So I'll need to get all the info in the industry set, all the info in the application set etc for product page a, then for product page b, and so on.
And it needs to grab this via the attribute set, not the individual attribute handles, as that needs to be extendable in the future (but the sets are, well, set).

But I've no idea how to display all the information from the page attribute sets of another page.
Any help would be greatly appreciated.

 
hutman replied on at Permalink Reply
hutman
What you could do is generate an array of all the attribute handles in the set that you are trying to use by doing something like this:

$attributeSetHandles = array();
$attributeSet = AttributeSet::getByHandle('ATTRIBUTE_SET_HANDLE');
if($attributeSet){
    $attributeKeyList = $attributeSet->getAttributeKeys();
    if(count($attributeKeyList) > 0){
        foreach($attributeKeyList as $attributeKey){
            $attributeSetHandles[] = $attributeKey->akHandle;
        }
    }
}

Then as you are looping over your pages to display them you would just add in another loop to go through and output that page's attribute values, like this:

if(count($attributeSetHandles) > 0){
    foreach($attributeSetHandles as $handle){
        echo $page->getAttribute($handle);
    }
}
ErikM replied on at Permalink Reply
Hey, thanks for the reply, that feels like it's pointing me in the right direction.

Not every page will be using the same attributes in each set though (Product A might have industries 1, 2, 4, 5, while product B might have industries 1, 3, 6 and 6); the Industry and Application set attributes are just auto-filled checkboxes for the purpose of the filter page being able to say 'this page has these checkboxes ticked'.

Would that array work for this purpose, or would I need another method (arrays have always been a weak-point for me so I'm never confident with them)?

Cheers.
hutman replied on at Permalink Reply
hutman
I'm sorry, your description just doesn't give me a clear picture of what you're trying to achieve or what your Attribute Sets, Attributes, and Attribute Values are so I can help you further.
ErikM replied on at Permalink Reply
I guess I was trying not to go overboard on details that might've just clouded the issue.

Relevant bits;

Bunch of product pages. Products are various types of industrial cameras.
I'm using attribute sets on these pages for data the filter page will be looking for.
My sets are;
Technical - the attributes in this set are text entries because of the wide variety of possible values. Attributes include Type, Frames per Second, Resolution.
Application - these are auto-selected checkboxes covering what the cameras are used for; ballistics, impact studies, material research and so on.
Industry - these are auto-selected checkboxes covering what sectors the cameras are used in; medical, automotive, military and so on.

The filter page will have a list of the products as a sidebar.
Clicking on each product will list out the relevant information about the product in the main body of the page; what type of camera, its FPS and resolution, what applications it's suitable for, which industries it's commonly used in.

So I need to be able to, on the filter page, say 'for page id 188, display all values in the Technical attribute set, all selected checkboxes in the Application attribute set, and all selected checkboxes in the Industry attribute set'.

I just can't figure out how to access that information from another page.
Hopefully that's a bit clearer.
Thanks again for your time.
hutman replied on at Permalink Reply
hutman
Ok, so still generate the array of all the handles/labels for this attributeSet by doing this:

$attributeSetHandles = array();
$attributeSet = AttributeSet::getByHandle('ATTRIBUTE_SET_HANDLE');
if($attributeSet){
    $attributeKeyList = $attributeSet->getAttributeKeys();
    if(count($attributeKeyList) > 0){
        foreach($attributeKeyList as $attributeKey){
            $attributeSetHandles[$attributeKey->akHandle] = $attributeKey->akName;
        }
    }
}

Then as you are looping over your pages to display them you would just add in another loop to go through and output that page's attribute values if they exist, like this:

if(count($attributeSetHandles) > 0){
    foreach($attributeSetHandles as $handle => $label){
        $pageAttributeValue = $page->getAttribute($handle, 'display');
        if(strlen(trim($pageAttributeValue)) > 0){
            echo $label .' = '.$pageAttributeValue;
        }
    }
}

One thing to note is that if you have a multi-select attribute (checkboxes) the $pageAttributeValue will contain <br /> tags, so if you don't want them outputting that way you'll have to do a replace.
ErikM replied on at Permalink Reply
In case anyone in future stumbles across this looking for the same thing, I found the answer in another discussion;

$d = PAGE::getByID($page_id);                
$page = $d->getCollectionName();
echo '<p>'.$page.'</p>';
$attr_set = AttributeSet::getByHandle('technical');
$attr_keys = $attr_set->getAttributeKeys();
foreach($attr_keys as $ak) {
        echo '<p>'.$ak->akName.'</p>';
        echo '<p>'.$d->getAttribute($ak->akHandle).'</p>';
 };


Many thanks to hutman for giving time and attention to this, anyway.