0

We want to use a hook_node_presave to set the Organic Group (OG) field on the basic page content type based on the node's language. We have created several OGs using nodes and each one is assigned a language so there is a mapping between Language to OG based on the OG node.

So basically, when the basic pages' language is set to en, then the OG with a language of en, will be assigned to the basic page's OG field.

Current nodes:

OG1 --Title = test 1 --language = en

OG2 --Title = test 2 --language = en-gb

OG3 --Title = test 3 --language = fr

basic page1 --title = some title1 --language = en --og_field = ??

basic page2 --title = some title2 --language = fr --og_field = ??


Expected result when the hook_node_presave is done:

basic page1 --title = some title1 --language = en --og_field = OG1

basic page2 --title = some title2 --language = fr --og_field = OG3


here is what i have tried, but not sure why it is not working (assume organic_group is the content type machine name)

function mymodule_node_presave(&$node) {

    if($node->type == 'page') {
        //get the OG node that has the same language as the current node
        $result = db_select('node', 'n')
           ->fields('n', array('nid', 'language', 'og_field'))
           ->condition('n.type', 'organic_group')
           ->condition('n.language', $node->language)
           ->execute()
           ->fetchAll();

        //store OG node's nid into og_field
        $node->og_field = $result->nid; 
    }
  }

1 Answer 1

0

Using Rules, you can add an event for when a new node is saved, add conditions for both content type and language and then use an action for assigning the OG's nid to the og_group_ref field.

Use set a data value if cardinality of the node is set to 1 or add to list for more than 1.

Without rules or code, I would have a new node link in each organic group and prepopulate the language and og_group_ref fields.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.