You are here

function linkit_field_element_after_build in Linkit 7.3

After build callback.

Parameters

array $element: Form API element.

array $form_state: State of form the element belongs to.

Return value

array Form API element with attached Linkit functionality.

1 string reference to 'linkit_field_element_after_build'
linkit_element_info_alter in ./linkit.module
Implements hook_element_info_alter().

File

./linkit.field.inc, line 201
Implementation for Fields and Linkit.

Code

function linkit_field_element_after_build(array $element, array &$form_state) {

  // Default settings.
  $settings = array(
    'profile' => NULL,
    'enable' => TRUE,
    'button_text' => t('Search'),
    'js_settings' => array(),
  );

  // For Field API elements, merge settings from field instance.
  if (isset($element['#entity_type'])) {
    $instance = field_info_instance($element['#entity_type'], $element['#field_name'], $element['#bundle']);
    if (isset($instance['settings']['linkit'])) {
      $settings = array_merge($settings, $instance['settings']['linkit']);
    }

    // Special treatment for link fields.
    if ($element['#type'] == 'link_field') {
      $settings['js_settings']['source'] = $element['url']['#id'];

      // @see link_field_info()
      // @see link_field_instance_settings_form()
      //
      // Link fields have a title field, but value could
      // be changed only for those options.
      if (in_array($instance['settings']['title'], array(
        'optional',
        'required',
      ))) {
        $settings['js_settings']['titleField'] = $element['title']['#id'];
      }
    }
  }

  // Allow dynamically adding to any eligible element type.
  if (!empty($element['#linkit'])) {
    $settings = array_merge($settings, $element['#linkit']);
  }

  // If profile is empty or disabled, skip the rest.
  if (empty($settings['profile']) || empty($settings['enable'])) {
    return $element;
  }

  // Load the profile.

  /* @var \LinkitProfile $profile */
  $profile = linkit_profile_load($settings['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 = $settings['js_settings'] + array(
    'helper' => 'field',
    'source' => $element['#id'],
    'profile' => $profile->name,
    'insertPlugin' => $profile->data['insert_plugin']['plugin'],
  );

  // Add Linkit dialog button to the element suffix.
  $element['#field_suffix'] = l($settings['button_text'], '', 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(
          $element['#id'] => $js_settings,
        ),
      ),
    ),
  );
  return $element;
}