You are here

function linkit_dashboard_form in Linkit 7.3

Same name and namespace in other branches
  1. 7.2 linkit.module \linkit_dashboard_form()

Create the dashboard page.

1 string reference to 'linkit_dashboard_form'
linkit_dashboard_page in ./linkit.module
Creates the dialog dashboard.

File

./linkit.module, line 557
Main file for Linkit module.

Code

function linkit_dashboard_form($form, &$form_state) {

  // The use of a form here is insignificant as we are not submitting any data.
  // A form here just cause trouble because it can be submitted and will do new
  // request to the action of the form.
  // In normal cases this isn't really a problem as the javascript is preventing
  // the form to be submitted, but in case of a javascript error, this event
  // can't be prevented.
  // We still have to use the FAPI to build this "form", as we want proper
  // classes and ids on the elements and take advantage of all the good parts of
  // the form generation.
  // So, just make sure the form tags dont get rendered.

  //$form['#theme_wrappers'] = array();

  // Get the active Linkit profile.
  if (!empty($form_state['values']['profile'])) {
    linkit_set_active_profile(linkit_profile_load($form_state['values']['profile']));
  }
  $active_profile = linkit_get_active_profile();
  $profiles = linkit_profile_editor_load_all();
  $_profiles = array();
  foreach ($profiles as $profile) {
    $_profiles[$profile->name] = check_plain($profile->admin_title);
  }

  // Attach css to the form.
  $form['#attached']['css'][drupal_get_path('module', 'linkit') . '/css/linkit.css'] = array(
    'preprocess' => FALSE,
  );

  // Attach js to the form.
  $form['#attached']['js'] = array(
    drupal_get_path('module', 'linkit') . '/js/linkit.dashboard.js',
  );
  if ($active_profile->profile_type == LINKIT_PROFILE_TYPE_EDITOR) {
    $form['profile'] = array(
      '#type' => 'radios',
      '#title' => t('Select profile to use'),
      '#default_value' => $active_profile->name,
      '#weight' => -100,
      '#options' => $_profiles,
      '#ajax' => array(
        'callback' => 'linkit_change_profile',
        'wrapper' => 'linkit-dashboard-ajax-wrapper',
        'method' => 'replaceWith',
        'effect' => 'fade',
        'event' => 'click',
      ),
      '#prefix' => '<div id="linkit-profile-changer">',
      '#suffix' => '</div>',
    );
    foreach ($profiles as $profile) {
      $form['profile'] += array(
        $profile->name => array(
          '#description' => check_markup($profile->admin_description),
        ),
      );
    }
  }
  $form['linkit_container'] = array(
    '#type' => 'container',
    '#weight' => 100,
    '#prefix' => '<div id="linkit-dashboard-ajax-wrapper">',
    '#suffix' => '</div>',
    '#attached' => array(
      'js' => array(
        array(
          'data' => array(
            'linkit' => array(
              'currentInstance' => array(
                'profile' => $active_profile->name,
                'autocomplete' => array_filter($active_profile->data['autocomplete']),
              ),
            ),
          ),
          'type' => 'setting',
        ),
      ),
    ),
  );
  $form['linkit_container']['linkit_search'] = array(
    '#type' => 'textfield',
    '#title' => t('Search for content.'),
    '#description' => t('Start typing to find content or paste a URL.'),
    '#maxlength' => 255,
    '#size' => 60,
    '#default_value' => '',
    '#weight' => -10,
    '#attributes' => array(
      'class' => array(
        'linkit-search-element',
      ),
    ),
    '#attached' => array(
      'library' => array(
        array(
          'linkit',
          'bac',
        ),
      ),
    ),
  );
  $form['linkit_container']['linkit_path'] = array(
    '#type' => 'textfield',
    '#title' => t('Link URL'),
    '#description' => t('This will be populated by the search, or you can fill it in yourself.'),
    '#required' => TRUE,
    '#maxlength' => NULL,
    '#size' => 60,
    '#default_value' => '',
    '#weight' => -1,
    '#attributes' => array(
      'class' => array(
        'linkit-path-element',
      ),
    ),
  );

  // If we have enabled attributes, lets put them inside a fieldset.
  if ($active_profile
    ->getEnabledAttributePlugins()) {

    // Create the container fieldset.
    $form['linkit_container']['linkit_attributes'] = array(
      '#type' => 'fieldset',
      '#title' => t('Options'),
      '#collapsible' => TRUE,
      '#collapsed' => TRUE,
      '#weight' => 10,
      '#attributes' => array(
        'class' => array(
          'linkit-attributes',
        ),
      ),
    );

    // Append the attributes info the fieldset.
    foreach ($active_profile
      ->getEnabledAttributePlugins() as $name => $attribute) {

      // Add a class to all items.
      $attribute['#attributes']['class'][] = 'linkit-attribute-' . $name;

      // Add 'linkit_' prefix to ensure that is unique.
      $form['linkit_container']['linkit_attributes']['linkit_' . $name] = $attribute;
    }
  }
  $form['linkit_container']['linkit_insert'] = array(
    '#type' => 'button',
    '#value' => t('Insert link'),
    '#suffix' => '<a id="linkit-cancel" href="#">' . t('Cancel') . '</a>',
    '#weight' => 100,
    '#attributes' => array(
      'class' => array(
        'linkit-insert',
      ),
    ),
  );
  return $form;
}