I am using the relation module and i don't like the way the default relation blocks work(entity collector and relation add blocks).

SO I'm trying to write my own block for creating relation. I have a form which has two text boxes, where the user selects two different end points between which the relation has to be made. he then clicks on submit and the relation has to be created.

coming to the relation module, i see that it doesn't have an API for use. Also i looked at the existing relation creating blocks(entity collector and relation add), they load entities and not nodes. Can some one explain me how i load all the entities available. Also is there a way where I can get the entity from the node id ?


  • It would be intersting to know how you came to the conclusion that Relation "doesn't have an API for use". As being an API is the modules primary function, there's a fairly severe documentation problem where ever you got that idea from. – Letharion May 23 '12 at 18:42
  • I'm sorry that my answer may be somewhat incomplete. Your title and last sentence form a fairly clear question. Your third paragraph however makes no sense to me. – Letharion May 23 '12 at 18:45

I have a module that uses Hierarchical Select to give users a nice looking UI to create relations from.

Below is an excerpt of how I create the relation.

  $tid = 1; // A term id.
  $node = node_load(1);
  $relation_bundle = 'a_type_of_relation';

  $endpoints = array();
  $endpoints[] = array('entity_type' => 'taxonomy_term', 'entity_id' => $tid);
  $endpoints[] = array('entity_type' => 'node', 'entity_id' => $node->nid);

  $new_relation = relation_create($relation_bundle, $endpoints);

  if ($rid = relation_save($new_relation)) {
    drupal_set_message(t('Relation created'), 'status', FALSE);
  else {
    drupal_set_message(t('Creating relation failed'), 'warning', FALSE);

Adding more useful validation and messages is left as an exercise to reader, as that will be very context sensitive.

  • in the code shouldn't $assignment be $new_relation ? – Jayaram May 23 '12 at 21:51
  • Yes, absolutely, missed that when I made the code look more generic from my original. I fixed it now. – Letharion May 24 '12 at 6:05
  • Also do you the better way to moderate when relations are created ? In my site, everyone can create relations between entities but i want the people who created those nodes to moderate it. Can you give me an idea of how to do it ? – Jayaram May 25 '12 at 14:51
  • That sounds like a different question. :) – Letharion May 25 '12 at 16:42
  • yea it does.. but i already do ask a lot of questions :P so i thought i would just ask it here since the question is related to the context. – Jayaram May 25 '12 at 17:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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