'fieldset', '#title' => t('Linkit field settings'), '#collapsible' => FALSE, '#collapsed' => FALSE, ); // Enable Linkit on this field instance. $form['instance']['settings']['linkit']['enable'] = array( '#type' => 'checkbox', '#title' => t('Enable Linkit for this field.'), '#default_value' => isset($instance['settings']['linkit']['enable']) ? $instance['settings']['linkit']['enable'] : 0, '#description' => t('Do not use this for CKeditor and TinyMCE fields. You will have to configure this on the wysiwyg/ckeditor profile.'), ); $profiles = linkit_profile_field_load_all(); $options = array(); foreach ($profiles as $profile) { $options[$profile->name] = $profile->admin_title; } // Sort the options. natsort($options); $form['instance']['settings']['linkit']['profile'] = array( '#type' => 'select', '#title' => t('Profile'), '#options' => $options, '#empty_option' => t('- Select a profile -'), '#default_value' => isset($instance['settings']['linkit']['profile']) ? $instance['settings']['linkit']['profile'] : '', '#states' => array( 'invisible' => array( 'input[name="instance[settings][linkit][enable]"]' => array('checked' => FALSE), ), 'required' => array( 'input[name="instance[settings][linkit][enable]"]' => array('checked' => TRUE), ), ), '#element_validate' => array('linkit_field_profile_validate'), ); // Enable Linkit on this field instance. $form['instance']['settings']['linkit']['button_text'] = array( '#type' => 'textfield', '#title' => t('Button text that activates linkit modal.'), '#default_value' => isset($instance['settings']['linkit']['button_text']) ? $instance['settings']['linkit']['button_text'] : t('Search'), ); } } /** * Validation callback; Only validate the profile field if linkit is enabled on * the instance. * * @see linkit_form_field_ui_field_edit_form_alter() */ function linkit_field_profile_validate($element, &$form_state, $form) { if (isset($form_state['values']['instance']['settings']['linkit']['enable']) && $form_state['values']['instance']['settings']['linkit']['enable']) { if (empty($element['#value'])) { form_error($element, t('You must select an profile.')); } } } /** * Process callback. */ function linkit_process_field_element($element, &$form_state, &$complete_form) { // Only proceed if the field is attached to an entity. if (!isset($element['#entity_type'])) { return $element; } $field = field_info_field($element['#field_name']); $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']); if (isset($instance['settings']['linkit']['enable']) && $instance['settings']['linkit']['enable']) { // Load the profile. $profile = linkit_profile_load($instance['settings']['linkit']['profile']); if (!$profile) { return $element; } // Load the insert plugin for the profile. $insert_plugin = linkit_insert_plugin_load($profile->data['insert_plugin']['plugin']); if ($insert_plugin === NULL) { return $element; } // Set the field ID. $field_id = $element['#id']; // Special treatment for link fields. if ($element['#type'] == 'link_field') { $field_id = $element['#id'] . '-url'; } $field_js = array( 'data' => array( 'linkit' => array( 'fields' => array( $field_id => array( 'profile' => $instance['settings']['linkit']['profile'], 'insert_plugin' => $profile->data['insert_plugin']['plugin'], 'url_method' => $profile->data['insert_plugin']['url_method'], // @TODO: Add autocomplete settings. ), ), ), ), 'type' => 'setting', ); // Link fields can have a title field. if ($element['#type'] == 'link_field') { if (isset($instance['settings']['title']) && in_array($instance['settings']['title'], array('optional', 'required'))) { $field_js['data']['linkit']['fields'][$field_id]['title_field'] = $element['#id'] . '-title'; } } // 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'][] = $field_js; $button_text = !empty($instance['settings']['linkit']['button_text']) ? $instance['settings']['linkit']['button_text'] : t('Search'); // Add Linkit dialog button to the element suffix. $element['#field_suffix'] = '' . $button_text . ''; } return $element; }