0

I do not wanna create another wheel but could not find a solution yet.

I have multi-value field (which I implemented myself) and I would like to have possibility to add 'new item' with already predefined values.

What would you recommend to do?

I googled and did not find anything suitable for me and considering to create my own module for this.

enter image description here

1 Answer 1

1

Consider using Entity References and not a complex compound field like your using now.

By using references you can use either the Inline Entity Form or the References Dialog modules to provide a more streamlined UI for adding new events from new popup windows. I was hoping you could reference exisitng events through a search using these modules (you can) but it won't clone the reference directly.

BUT, you could use the Replicate module, to replicated the entity reference you've selected to then edit it -- using custom code.

An example way to replicate a referenced entity can be seen in this replicate issue, as shown below:

/**
 * Replicate entities that are the target of entityreference fields 
 *
 * Implements hook_replicate_field_FIELD_TYPE
 *
 * For entites that have one or more entity reference fields we need to replicate the
 * referenced entities as well so the cloned parents don't all point to the same children.
 * We also need to update the entityreference fields to point to them.
 * 
 * @param object $replica
 *   Reference to the fully loaded entity object being saved (the clone) that
 *   can be altered as needed.
 * @param string $entity_type
 *   Type of the entity containing the field.
 * @param string $field_name
 *   Name of the field that is going to be processed.
 *
 * @see replicate_clone_entity()
 */

function MY_MODULE_replicate_field_entityreference(&$replica, $entity_type, $field_name) {

  // Only replicate entity reference field you are interested in
  if ($field_name == 'field_MY_ENTITYREFERENCE_FIELD') {

    // Wrap the replicated entity.
    $wrapper = entity_metadata_wrapper($entity_type, $replica);

    // Loop over the field values
    foreach ($wrapper->$field_name as $delta => $referenced_field_item_wrapper) {
      $referenced_entity = $referenced_field_item_wrapper->value();

      // Replicate the referenced entity and get the new id
      $replicated_referenced_entity_id = replicate_entity('node', $referenced_entity);

      // Give the replica reference field the new target id
      $replica->{$field_name}[LANGUAGE_NONE][$delta]['target_id'] = $replicated_referenced_entity_id;
    }
  }
}

So I'm saying Replicate and 1 of the many "dialog" modules could help make what you want to create more pretty. You could probably use the Replicate API and a custom button in your own form too -- if you want to do even more learning ... :)

Your Answer

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

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