I searched for quite some time to see if it was possible to add LinkIt to any form field, as I required this functionality.

I don't believe this is documented anywhere else (if I'm wrong please reply as there may be a tidier way to do it), so I wanted to leave a post to help anyone else that needs to do this.

Essentially what you'll want to do is use code from the linkit_process_field_element function in linkit.field.inc.

Below is a snippet of code that's working for me. The variables you'll need to change are the form element #id and the linkit profile (in my case it's named form_raw).

<?php
   $form['form_settings']['go_to_entity'] = array(
        '#type' => 'textfield',
        '#title' => t('File to load upon success'),
        '#default_value' => !empty($conf['go_to_entity']) ? $conf['go_to_entity'] : '',
        '#id' => 'go_to_entity_field',
    );

    $profile = linkit_profile_load('form_raw');

    // Load the insert plugin for the profile.
    $insert_plugin = linkit_insert_plugin_load($profile->data['insert_plugin']['plugin']);

    // Set the field ID.
    $field_id = 'go_to_entity_field';

    $field_js = array(
        'data' => array(
            'linkit' => array(
                'fields' => array(
                    $field_id => array(
                        'profile' => 'form_raw',
                        'insert_plugin' => $profile->data['insert_plugin']['plugin'],
                        'url_method' => $profile->data['insert_plugin']['url_method'],
                    ),
                ),
            ),
        ),
        'type' => 'setting',
    );

    // Attach js files and settings Linkit needs.
    $form['form_settings']['go_to_entity']['#attached']['library'][] = array('linkit', 'base');
    $form['form_settings']['go_to_entity']['#attached']['library'][] = array('linkit', 'field');
    $form['form_settings']['go_to_entity']['#attached']['js'][] = $insert_plugin['javascript'];
    $form['form_settings']['go_to_entity']['#attached']['js'][] = $field_js;

    $button_text = t('Search');
    $form['form_settings']['go_to_entity']['#field_suffix'] = '<a class="button linkit-field-button linkit-field-' . $field_id . '" href="#">' . $button_text . '</a>';

?>

Comments

vood002 created an issue.

martini9011’s picture

First of all thanks for the docs, really handy stuff! We are currently using Linkit version 3.5, and unfortunately this solution was not working for us. After changing this to the following we managed to get it to work again:

<?php

/**
 * Helper function _my_module_lists_add_linkit_search.
 */
function _my_module_lists_add_linkit_search($element, &$form_state, &$complete_form) {

  // Guard: Load the profile.
  $profile = linkit_profile_load('NAME_OF_LINKIT_PROFILE');
  if (!$profile || !isset($profile->data['insert_plugin']['plugin'])) {
    return $element;
  }

  // Load the insert plugin for the profile.
  $insert_plugin = linkit_insert_plugin_load($profile->data['insert_plugin']['plugin']);
  $js_settings = array(
    'helper' => 'field',
    'source' => $element['url']['#id'],
    'titleField' => $element['title']['#id'],
    'profile' => 'NAME_OF_LINKIT_PROFILE',
    'insertPlugin' => $profile->data['insert_plugin']['plugin'],
  );

  // Add Linkit dialog button to the element suffix.
  $element['#field_suffix'] = l(t('Search'), '', array(
    'attributes' => array(
      'class' => array(
        "button",
        "linkit-field-button",
        "linkit-field-{$js_settings['source']}",
      ),
    ),
  ));

  // Attach js files and settings Linkit needs.
  $element['#attached']['library'][] = array('linkit', 'base');
  $element['#attached']['library'][] = array('linkit', 'field');
  $element['#attached']['js'][] = $insert_plugin['javascript'];
  $element['#attached']['js'][] = array(
    'type' => 'setting',
    'data' => array(
      'linkit' => array('fields' => array($js_settings)),
    ),
  );

  return $element;
}

?>

Make sure you replace "NAME_OF_LINKIT_PROFILE" with the name of your Linkit profile.

Also, to trigger this on a field, I add the "#after_build" attribute in a form alter, so you get something like this:

$form['my_link_button']['field_my_link_button']['#after_build'][] = '_my_module_add_linkit_search';