1

There are two entity types on my site: 1) an application for participation (entityform) and 2) a participant profile (node). At first user creates an application for participation. And then site administrator must create somehow a new participant profile and fill its fields from an entityform created by user.

How to create such an action "Create participant profile from application"? The action must be accessible to administrator as a button or a menu tab item on a entityform view page.

0

If I have to do that, I would take these steps to achieve what you want here :

  • Make a custom module for it.
  • Add a path by help of hook_menu.
  • Make a custom function for menu callback & mention the same in there.
  • Inside function, get current node ID (entityform submission, where admin is there) by something like if (arg(0) == 'node' && is_numeric(arg(1))) $nodeid = arg(1);
  • Then write code to load a entity by ID by help of entity_load & take all the values in custom vars as per requirement for new node(profile).
  • Save the node of type profile by help of node_save.
  • Insert a link(CREATE PROFILE) mapped with created path(by above hook_menu) to the node view of entityform submission by help of hook_node_views.

Now when ever your inserted link will be clicked, a profile will be created.

You can restrict the access of created path by implementing hook_permission & give access to admin only.

You are free to do some other stuff as well, like if profile node exist for entityform submission them make link disappear as well. It all depends upon your need & plan :)

0

Thanks, RajeevK! I think I've found a simpler solution. Add an edit/add link to an entityform view page:

function MODULE_menu_local_tasks_alter(&$data, $router_item, $root_path) {
  if ($root_path === 'entityform/%') {
    $entityform = $router_item['page_arguments'][0];
    if ($entityform->type === 'onfit_awards2014_join') {
      // Check whether a participant profile was created already.
      // field_application_form is a back reference from a participant profile
      // to a source appication form
      $participant_id = NULL;
      $query = new EntityFieldQuery();
      $query->entityCondition('entity_type', 'node')
        ->entityCondition('bundle', 'onfit_awards_participant')
        ->fieldCondition('field_application_form', 'target_id', $entityform->entityform_id)
        ->range(0, 1);
      $result = $query->execute();
      if (is_array($result['node']) && count($result['node'])) {
        $participant_id = array_shift(array_keys($result['node']));
      }

      // Create either an edit link or an add link
      if (!empty($participant_id)) {
        $link = menu_get_item('node/' . $participant_id . '/edit');
        $link['title'] = 'Правка профиля участника';
      }
      else {
        $link = menu_get_item('node/add/onfit-awards-participant');
        $link['title'] = 'Создать профиль участника';
        $link['localized_options']['query'] = array(
          'entityform' => $entityform->entityform_id,
        );
      }
      $data['tabs'][0]['output'][] = array(
        '#theme' => 'menu_local_action',
        '#link' => $link,
      );
      $data['tabs'][0]['count']++;
    }
  }
}

Copy field values from an application form to a new participant profile:

function MODULE_node_prepare($node) {
  if (empty($node->nid)) {
    // Find a query parameter of the form:
    // ?entity_type=entity_id
    $params = drupal_get_query_parameters();
    $entity_types = entity_get_info();
    $entity_type = NULL;
    $entity_id = NULL;
    foreach ($params as $key => $value) {
      if (is_numeric($value) && !empty($entity_types[$key])) {
        $entity_type = $key;
        $entity_id = $value;
        break;
      }
    }

    // If entity_type and entity_id were specified, copy all field values
    // from the source entity to a current node
    if (!empty($entity_type) && !empty($entity_id)) {
      $entityform = entity_metadata_wrapper($entity_type, $entity_id);
      $entityform_type = $entityform->type->value()->type;
      $node_wrapper = entity_metadata_wrapper('node', $node);
      $src_fields = field_info_instances($entity_type, $entityform_type);
      $dest_fields = field_info_instances('node', $node->type);
      foreach ($dest_fields as $name => $desc) {
        if (!empty($src_fields[$name])) {
          $value = $entityform->$name->value();
          $node_wrapper->$name->set($value);
        }
      }

      // Some custom logic
      if ($entityform_type === 'onfit_awards2014_join' &&
          $node->type === 'onfit_awards_participant') {
        $last_name = $entityform->field_last_name->value();
        $first_name = $entityform->field_first_name->value();
        $node_wrapper->title->set($last_name . ' ' . $first_name);
        $node_wrapper->field_application_form->set($entity_id);
      }
    }
  }
}

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.