I propose this patch, which allow you to specify in your fields formatter two parameters : 'suitability callback', and 'suitability callback arguments'
Thus on the zone tab, you will have in front of your nodereference fields only the fields formatter which can apply to the referenced node.
For instance if you define the following field formatter :
function modulecustom_field_formatter_info() {
return array(
'my_formatter' => array(
'label' => t('Format custom'),
'field types' => array('nodereference'),
'multiple values' => CONTENT_HANDLE_CORE,
'suitability callback' => '_is_formatter_suitable',
'suitability callback arguments' => array('type' => 'image'),
),
}
with the following simple check function :
function _is_formatter_suitable($value, $args = array()) {
//if there are multiple node references we choose to leave all formatters
if(count($value) > 1) {
return TRUE;
}
if (isset($value[0])) {
$value = $value[0];
}
if (is_array($value)) {
if (is_numeric($value['nid'])) {
$nid = $value['nid'];
}
else if (is_array($value['nid']['nid'])) {
preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $value['nid']['nid']['#value'], $matches);
$nid = $matches[2];
}
else if (!empty($value['nid']['nid'])) {
preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $value['nid']['nid'], $matches);
$nid = $matches[2];
}
}
if ($node = node_load($nid)) {
if ($node->type == $args['type']) {
return TRUE;
}
}
return FALSE;
}
The field formatter select will propose 'Format custom' only in front of image.
I took most of the aef_formatter_selector module's code
| Comment | File | Size | Author |
|---|---|---|---|
| composite_cck.patch | 5.08 KB | raphael waeselynck |
Comments
Comment #1
raphael waeselynck commentedSorry, I forgot the tags to format my code. So here it is :
and
Comment #2
sdelbosc commentedJust to track the issue...
Comment #3
bengtan commentedHi,
As I understand it ... your patch allows for certain cck formatters to be excluded from the 'Display as' dropdown list (on the Zones page) depending on certain programmatic conditions.
Looks okay except for one thing ...
I'm never seen the 'suitability callback' and 'suitability callback arguments' properties in cck formatter definitions before. Is this a new or recent CCK feature?
If they are a soon-to-be standard part of the cck api, then that's okay. If they are new properties that you've created ... I'm a bit more hesistant.
Comment #4
raphael waeselynck commentedIndeed, they are custom properties. I've never heard of this kind of properties, and for me it's a lack. How would you suggest to deal with that ?
Regards,
Raphael
Comment #5
bengtan commentedHi,
Firstly, I'm not totally clear on what scenario you're trying to address. It seems like you have nodereference fields which you would like to be displayed according to whether the referenced node is an image, a page, a story node etc.
If so, then it might be more correct to pursue this issue with the cck nodereference module, because it's really a cck issue. Composite Layout just provides a wrapper around CCK.
Perhaps if you can give me more information on your situation and what you're trying to accomplish, I can think on it more. However, it may still be an issue best pursued with the cck module maintainers.
Comment #6
raphael waeselynck commentedYou're totally right about both what I'd like to do and what I have to do :
I'll submit the issue as a new feature in a few days for cck module.
Nevertheless the patch still applies to composite_cck...
Thanks for reading and for your advices.
Raphael
Comment #7
raphael waeselynck commentedComment #8
raphael waeselynck commentedthe patch still applies to composite_cck...
Comment #9
sdelbosc commentedCck and composite do not use cck field formatters exactly in the same way. When cck uses formatters it does not know the node which will be formatted whereas composite knows it.
That is the reason why I think cck cannot be patched to add such feature.
Comment #10
bengtan commentedMy thoughts:
I think this issue has asked the wrong question. The correct question should perhaps be:
I would like my CCK node reference field to use different field formatters depending of the node type of the referenced node. How can we make node reference fields to allow for this?
At present, this may or may not be possible without making big changes to nodereference, but I think the correct location to implement such a feature is in CCK, not in Composite Layout. At least, it should be considered as the preferred location.
Secondly, the original author suggested adding extra 'properties' to CCK field formatters. I'm hesistant to use these extra properties if they are non-standard (and may clash with, or duplicate, future properties).
Thirdly, as a last resort, if this approach doesn't work, the original author may like to explore other possible solutions ... but this is an open-ended scope and may be best done as a custom commercial solution.
Comment #11
bengtan commentedExpiring this issue as it is old and uncertain whether it is still applicable.