Database Autofill Fields in Drupal Content Type

I started creating a new type of content with the Field Group and Field Collection modules, and it worked!

But I would like to add autocomplete fields to my form, and I do not find how to do it. Perhaps using hook_form_alter, however, I cannot add my custom fields to my field collection.

Do you know how to do this?

Thank you so much

PS: I work with D7

+3
source share
2 answers

To add auto_complete to the from field, you need to do the following:

1st: use hook_for_alter () to add the auto_complete path to the text field

function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id)
{
    if($form_id == "YOUR-CONTENT-TYPE_node_form")
    {
        $form['field_YOUR-FIELD']['und'][0]['value']['#autocomplete_path'] = 'my-module/autocomplete/path';
    }
}

2nd: hook_menu(), , , #autocomplete_path

function YOUR_MODULE_menu()
{
    $items = array();
    $items['my-module/autocomplete/path'] = array(
    'page callback'     => 'your_module_autocomplete_callback',
    'access callback'   => TRUE,
    'weight'            => 1,
    'type'              => MENU_CALLBACK,
    );
    return $items;
}

3rd last: ,

function your_module_autocomplete_callback($string)
{
    $items = array();
    $query = db_select('node', 'n');
    $value = $query->fields('n', array('title'));
    $value = $query->condition(db_and()->condition('n.type', 'YOUR_CONTENT_TYPE')->condition('title', '%' . db_like($string) . '%', 'LIKE'))->orderRandom()->execute();

    $i = 0;
    foreach ($value as $val)
    {
        $items[$val->name] = check_plain($val->name);
    }
    print drupal_json_output($items);
    exit();
}

, ... .

+3

, .

function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id)
{
    if($form_id == "YOUR-CONTENT-TYPE_node_form")
    {
        foreach($form['field_match']['und'][0]['field_adversaires']['und'] as $k =>$v)
        {
            if(is_numeric($k))
            {
                $form['field_match']['und'][0]['field_adversaires']['und'][$k]['value']['#autocomplete_path'] = 'basketfacile_type/autocomplete';
            }
        }
    }
}

, ... .

+1

All Articles