1

Is there is process/ function that maps data types that you can use in entities (specifically custom entities using the Entity API) to form #type options? This is to dynamically add a field to a form from the entity and then once I've got the value from the submit handler, use the wrappers to set the property on a entity of the same entity type. Or am I taking the wrong approach here?

I am trying to get an entity property type and am currently using...

$property_info = entity_get_all_property_info('MY ENTITY TYPE');

Which generates $property_info['field_postcode'] which has this value...

field_postcode      Array [10]  
    label               Post Code   
    type                text    
    description         Field "field_postcode". 
    getter callback     entity_metadata_field_property_get  
    setter callback     entity_metadata_field_property_set  
    access callback     entity_metadata_field_access_callback   
    query callback      entity_metadata_field_query 
    translatable        false   
    field               true    
    required            true    

There I can see that it's got a 'type' of text which seems to translate well to a textfield in the Form API. But what about something like "file" in the Form API?

I wonder if I'm missing a piece of the puzzle here.

3
  • Unless someone's (quietly) made some really big strides somewhere, the answer will be no. Even trying to render the related widget for a field is a nightmare: drupal.stackexchange.com/questions/25140/…. There's just no framework for that unfortunately
    – Clive
    Commented May 13, 2014 at 16:32
  • OK, thanks @Clive. If I was to work on this and make it available for more capable Drupal.org people who might be interested to clean it up, would there be a good way of approaching it? I imagine there might be some kind of O-O/wrapper/Drupal-friendly way of taking the main steps forward.
    – ja_him
    Commented May 14, 2014 at 8:37
  • 1
    Just my opinion, but I think the best way (if it's at all possible) would be to approach this by way of a contributed module, sandboxed first of course; if it can avoid core patches that would obviously be a big win. Posting an answer here with a link to the module (plu a wee bit of description) would get it some exposure (I'd certainly try to help out with something like that if I could, and I'm sure others here would), but for maximum exposure opening an issue on Drupal.org, maybe against the Entity module if it ends up making sense, would be good
    – Clive
    Commented May 14, 2014 at 8:53

1 Answer 1

0

In response to Clive's comments under my question I have setup a project on drupal.org and issue to the Entity module issue queue as adding this functionality appears to be the best action. This would be the first contribution I've made so I'm eager for others to take on co-maintenance.

After I've thought a bit on how to go about doing this (still very open to suggestions) I'll add code etc. For the time being Clive's suggestion of using Entity module might be right but I've not yet checked it all out.

In the meantime, here is the code I was using (just as a fairly straight-forward form callback) with a form and call_user_func_array() passing through the callback the user would have used. That's just in a basic custom module I called entity_to_form because tbh that's all I know how to do at the moment).

code:

/**
 * Form callback
 * 
 * @param entity type, existing entity type as in entity_load() $entity_type
 * @param callback, as in call_user_func_array() $callback
 * @param array, as in call_user_func_array() $param_arr
 */
function entity_to_form_new_form($form, &$form_state, $entity_type, $callback) {
    // if not a valid entity type, set error and return
    $info = entity_get_info($entity_type);
    if (!is_array($info)) {
        global $user;
        $variables = array(
            '%uid' => $user->uid,
            '%timestamp' => time(),
            '%callback' => $callback,
            '%param_arr' => json_encode($param_arr),
        );
        watchdog('entity_to_form', 'callback not existing: error during function 
            entity_to_form_new_form() timestamp = %timestamp, uid = %uid, 
            callback = %callback, param_arr = %param_arr', $variables, 
            WATCHDOG_ERROR, $link = NULL);
        drupal_set_message('An error occurred and processing did not complete', 
            'error');
        return;
    }
    // if not a valid callback, set error and return
    if (!function_exists($callback)) {
        global $user;
        $variables = array(
            '%uid' => $user->uid,
            '%timestamp' => time(),
            '%callback' => $callback,
            '%param_arr' => json_encode($param_arr),
        );
        watchdog('entity_to_form', 'callback not existing: error during function 
            entity_to_form_new_form() timestamp = %timestamp, uid = %uid, 
            callback = %callback, param_arr = %param_arr', $variables, 
            WATCHDOG_ERROR, $link = NULL);
        drupal_set_message('An error occurred and processing did not complete', 
            'error');
        return;
    }
    // get information about the entity properties and custom fields
    $property_info = entity_get_all_property_info($entity_type);

    // dynamically set elements for the properties and custom fields

    // fullfil the final requirements for the submit handler
    $form['entity-to-form']['submit-callback'] = array(
        '#type' => 'value',
        '#value' => $callback,
    );
    $form['entity-to-form']['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Submit',
    );
    return $form;
}

/**
 * Form submit handler
 */
function entity_to_form_new_form_submit($form, &$form_state) {
    $callback = $form_state['values']['submit-callback'];
    $variables = '';
    call_user_func_array($callback, $variables);
}

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.