0

I have 4 entity types. All types have a relation to each other. For now let's call them A, B, C and D. How can I allow a user (let's say userA) to create an entity of type A so that entities for B, C and D are also created at the same moment. UserA has the permission to create A entity types and is not the author of the automatically created entities of type B, C and D. Is there a way to automatically create these related entities?

  • I am sure this can be done with the Rules module, however without knowing exactly the entity types, the fields on the entities and how the relationships are defined, it is difficult to give you a proper answer. – Nigel Waters Jul 3 '13 at 3:50
  • There was kinda similar questions already. Not a dup, but worth seeing for reference. – Mołot Jul 3 '13 at 12:33
  • @Molot yes there is but since it appeared that I was asking too many things at once they wanted me to focus on 1 thing only. You can read it here: drupal.stackexchange.com/questions/77657/… – BassPlaya Jul 4 '13 at 3:07
  • @NigelWaters entityA: a node containing one text field entityB: a node containing one text field entityC: a node containing one audio field entityD: a node containing one image field Please let me know if you need more info. Cheers! – BassPlaya Jul 4 '13 at 3:10
  • Ok. I'll give it a shot. Your first question was too long and drawn out and your second post is mossing some of the meaty details. Try for something in the middle next time. See my answer below. – Nigel Waters Jul 4 '13 at 4:59
0

If you were looking for a code solution, you can use hook_entity_insert() to trigger the creation of the other entities.

Your custom module code would look something like this:

function mymodule_entity_insert($entity, $type) {
  if ($type == 'typeA') {
    // entity type A is being inserted
    global $user;

    // now create type B
    $entity_b = entity_create('type_b', array(
          'uid' => $user->uid,
          'type' => 'type_b', //bundle
          'field_my_field_c' => array(
            LANGUAGE_NONE => array(
              0 => array(
                'value' => 'foo!',
              ),
            ),
          ),
        ))->save();

    // now create type C
    $entity_c = entity_create('type_c', array(
          'uid' => $user->uid,
          'type' => 'type_c', //bundle
          'field_my_field_c' => array(
            LANGUAGE_NONE => array(
              0 => array(
                'value' => 'foo!',
              ),
            ),
          ),
        ))->save();

    // now create type D, etc...
    }
}

I've borrowed from the code sample posted in the comment from Mołot and for simplicity sake, I made the author of A to be the same as B, C and D.

One more thing, the entity_create() function is in the contrib module Entity API. Your custom module would have a dependency on Entity API.

  • Thank you! I'll have a look at this code as I'm not sure how to build a custom module of any kind. This is completely new to me and I don't have a real background of php knowledge but I'll still give it a shot! Cheers! One thing: I'm not sure how to make my module dependent on the Entity API module. – BassPlaya Jul 4 '13 at 3:13
  • Here's a starting point for creating a module: drupal.org/node/361112 You can declare the dependency in the module's .info file, but that's not essential. Just make sure you download and install Entity API. – cleaver Jul 4 '13 at 3:26
  • that's cool, @cleaver! I'll try. – BassPlaya Jul 21 '13 at 4:33
0

Create 4 content types or your own entities. Create a field for each of the other three entities on the parent entity using the entity reference module. Use the inline entity form module to allow users to create the other three entities directly in the parent entity. Limit it to just one allowed value. If you are also limiting which entity can be created by role then use the field permission module to show/ hide accordingly.

  • Thank you for your suggestion. I hope I can go ahead with this fairly soon. (have been busy on other stuff too, apologies). I'll come back to this post to let you know what I've experienced. Cheers! – BassPlaya Jul 21 '13 at 4:31
  • I've tried your approach and it works but actually if it was done automatically without the intervention of the author that would be better. As a matter of fact I've created it automatically using the Rules module but I now I'm stuck on how to reference the newly created entity in the reference field on the node created by the author. Please see my post – BassPlaya Oct 10 '13 at 4:31
0

Okay, I've worked it out with Rules and it works like a charm without the need for additional modules or additional php code.

Here's how to:

Step 1: create 1 generic user (fictitious author) for each content type

Step 2: create an empty (or prefilled) node for each of the 3 content types which need to be referenced and set the author to the generic user specific for that entity

Now in Rules create a new rule which acts on the event of creating a node of content type A and then add the following actions

Step 3: add an action: create a new entity for content type B where you set the title of the new entity with replacement patterns (as you like) and where you set the author to be the generic user of that content type B (use the direct input method: uid = "x") and provide a "created entity" variable label and name

Step 4: add an action: save the entity

Step 5: add an action: set the data value of the reference field on the node of content type A to that "created entity"

Repeat steps 3 to 5 for the content type C and D. You'll end up with 3 new nodes which all have a generic author and are immediately tied to the reference fields in the content of type A.

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.