You are here

function rules_link_form in Rules Link 7

Same name and namespace in other branches
  1. 7.2 rules_link.admin.inc \rules_link_form()

Generates the rules link editing form.

File

./rules_link.admin.inc, line 83
Generate a admin UI.

Code

function rules_link_form($form, &$form_state, $rules_link, $op = 'edit') {
  drupal_add_css(drupal_get_path('module', 'rules_link') . '/rules_link.css', 'file');
  if ($op == 'clone') {
    $rules_link->label .= ' (cloned)';
    $rules_link->name .= '_cloned';
  }
  $form['label'] = array(
    '#title' => t('Label'),
    '#type' => 'textfield',
    '#default_value' => isset($rules_link->label) ? $rules_link->label : '',
    '#description' => t('The human-readable name of this rules link'),
    '#required' => TRUE,
    '#size' => 30,
  );
  $form['name'] = array(
    '#type' => 'machine_name',
    '#description' => t('A unique machine-readable name for this rules link. It must only contain lowercase letters, numbers, and underscores and must not begin with a number.'),
    '#default_value' => isset($rules_link->name) ? $rules_link->name : '',
    '#disabled' => $rules_link
      ->hasStatus(ENTITY_IN_CODE) && $op != 'clone',
    '#required' => TRUE,
    '#maxlength' => 32,
    '#machine_name' => array(
      'exists' => 'rules_link_get_links',
      'source' => array(
        'label',
      ),
    ),
  );
  $entity_options = array();
  $entities = entity_get_info();
  foreach ($entities as $key => $entity) {
    $entity_options[$key] = t($entity['label']);
  }
  $default_entity = isset($form_state['values']['entity_type']) ? $form_state['values']['entity_type'] : (isset($rules_link->entity_type) ? $rules_link->entity_type : 'node');
  $form['entity_type'] = array(
    '#title' => t('Entity Type'),
    '#description' => t('Enity type to which the link should be attached to.'),
    '#default_value' => $default_entity,
    '#options' => $entity_options,
    '#required' => TRUE,
    '#type' => 'select',
    '#maxlength' => 32,
    '#ajax' => array(
      'callback' => 'rules_link_bundle_js_callback',
      'wrapper' => 'rules-link-bundle',
      'speed' => 'fast',
    ),
  );
  $bundle_options = array();
  if (isset($form_state['clicked_button']) && $form_state['clicked_button']['#parents'][0] == 'show_all') {
    foreach ($entities as $key => $entity) {
      foreach ($entity['bundles'] as $key => $bundle) {
        $bundle_options[$key] = $bundle['label'];
      }
    }
  }
  else {
    foreach ($entities[$default_entity]['bundles'] as $key => $bundle) {
      $bundle_options[$key] = isset($bundle['label']) ? $bundle['label'] : $key;
    }
  }
  $form['bundle_wrapper'] = array(
    '#prefix' => '<div id="rules-link-bundle">',
    '#suffix' => '</div>',
  );
  $form['bundle_wrapper']['bundles'] = array(
    '#title' => t('Bundles'),
    '#description' => t('Bundles to which the link should be attached to. If left empty, the link is not restricted for any bundles.'),
    '#type' => 'select',
    '#options' => $bundle_options,
    '#default_value' => isset($rules_link->settings['bundles']) ? $rules_link->settings['bundles'] : array(),
    '#multiple' => TRUE,
  );

  // If the entity type does not make use of bundles, make sure we are not
  // restricting by bundle.
  $entity_info = entity_get_info($default_entity);
  if (empty($entity_info['entity keys']['bundle'])) {
    $form['bundle_wrapper']['bundles']['#access'] = FALSE;
    $form['bundle_wrapper']['bundles']['#value'] = array();
  }
  $form['bundle_wrapper']['entity_link'] = array(
    '#type' => 'checkbox',
    '#title' => t('Show link when entity is rendered'),
    '#description' => t('If checked, the link is shown when viewing an entity to which the bundle and the visibility conditions apply. Only applies to displayed entity types and if no addition variables were added.'),
    '#default_value' => isset($rules_link->settings['entity_link']) ? $rules_link->settings['entity_link'] : FALSE,
  );
  $view_modes = array();
  foreach ($entity_info['view modes'] as $machine_name => $view_mode) {
    $view_modes[$machine_name] = $view_mode['label'];
  }
  $form['bundle_wrapper']['view_mode'] = array(
    '#type' => 'select',
    '#title' => t('View mode'),
    '#description' => t('Select on or more view modes on which to show the link. If left empty, the link will shown on all view modes.'),
    '#default_value' => isset($rules_link->settings['view_mode']) ? $rules_link->settings['view_mode'] : '',
    '#multiple' => TRUE,
    '#options' => $view_modes,
    '#states' => array(
      // Show the form only when the link type is confirm.
      'visible' => array(
        ':input[name="entity_link"]' => array(
          'checked' => TRUE,
        ),
      ),
    ),
  );

  // Only allow to set the view modes settings, if there are view modes
  // available.
  if (empty($view_modes)) {
    $form['bundle_wrapper']['view_mode']['#access'] = FALSE;
    $form['bundle_wrapper']['view_mode']['#value'] = array();
  }
  $html_id_show_all = drupal_html_id('rules-link-show-all-wrapper');

  // Button for users without js.
  $form['show_all'] = array(
    '#type' => 'button',
    '#value' => t('Show All'),
    '#prefix' => "<div id='{$html_id_show_all}'>",
    '#suffix' => '</div>',
  );

  // Hide the button for users with javascript.
  drupal_add_js('(function ($) { $("#' . $html_id_show_all . '").hide() }(jQuery));', array(
    'type' => 'inline',
    'scope' => 'footer',
  ));
  $form['text'] = array(
    '#title' => t('Link text'),
    '#type' => 'textfield',
    '#default_value' => isset($rules_link->settings['text']) ? $rules_link->settings['text'] : '',
    '#description' => t('The text of the link.'),
    '#required' => TRUE,
    '#size' => 30,
  );
  $form['link_type'] = array(
    '#type' => 'radios',
    '#title' => t('Link type'),
    '#default_value' => isset($rules_link->settings['link_type']) ? $rules_link->settings['link_type'] : 'token',
    '#options' => array(
      'javascript' => t('Javascript'),
      'token' => t('Normal'),
      'confirm' => t('Confirmation'),
    ),
  );
  $form['confirm']['confirm_question'] = array(
    '#type' => 'textfield',
    '#title' => t('Confirm question'),
    '#default_value' => isset($rules_link->settings['confirm_text']) ? $rules_link->settings['confirm_text'] : '',
    '#required' => TRUE,
    '#weight' => 50,
    '#states' => array(
      // Show the form only when the link type is confirm.
      'visible' => array(
        'input[name="link_type"]' => array(
          'value' => 'confirm',
        ),
      ),
    ),
  );
  $form['confirm'] = array(
    '#type' => 'fieldset',
    '#title' => 'Confirmation form settings.',
    '#states' => array(
      // Show the form only when the link type is confirm.
      'visible' => array(
        'input[name="link_type"]' => array(
          'value' => 'confirm',
        ),
      ),
    ),
  );
  $form['confirm']['question'] = array(
    '#type' => 'textfield',
    '#title' => t('Confirm question'),
    '#description' => t('The question that should appear, when confirmation form is shown.'),
    '#default_value' => isset($rules_link->settings['confirm_question']) ? $rules_link->settings['confirm_question'] : '',
  );
  $form['confirm']['description'] = array(
    '#type' => 'textfield',
    '#title' => t('Confirm description'),
    '#description' => t('The description that should appear, when confirmation form is shown.'),
    '#default_value' => isset($rules_link->settings['confirm_description']) ? $rules_link->settings['confirm_description'] : '',
  );
  global $base_url;
  $form['path'] = array(
    '#title' => t('Path'),
    '#description' => t('(Optional) Path for the link. It not set a default link will be generated.'),
    '#default_value' => isset($rules_link->path) ? $rules_link->path : '',
    '#type' => 'textfield',
    '#field_prefix' => $base_url . '/',
  );
  if ($op == 'add') {
    RulesPluginUI::formDefaults($form, $form_state);
    $rules_link_temp_component = rules_plugin_factory('and');
    $rules_link_temp_component
      ->form($form['variables'], $form_state, array(
      'show settings' => TRUE,
      'button' => TRUE,
      'init' => TRUE,
    ));
    unset($form['variables']['settings']['label']);
    unset($form['variables']['settings']['name']);
    unset($form['variables']['settings']['tags']);
    $form['variables']['settings']['vars']['#title'] = t('Additional Variables');
    $form['variables']['settings']['vars']['#description'] = t('Here you can add additional variable, which should be used apart from the entity type by the access conditions and the rule sets.') . ' ' . $form['variables']['settings']['vars']['#description'];
    $form['variables']['settings']['#collapsible'] = FALSE;
    $form['variables']['settings']['#type'] = 'container';
    $form['variables']['settings']['label']['#default_value'] = '';
    $form['variables']['settings']['#weight'] = -1;

    // Add JS file that sets the correct type, label and machine name for the
    // selected entity type.
    drupal_add_js(drupal_get_path('module', 'rules_link') . '/rules_link.admin.js', 'file');
    $form['variables']['settings']['vars']['items'][0]['type']['#default_value'] = 'node';
    $form['variables']['settings']['vars']['items'][0]['type']['#disabled'] = TRUE;

    //$form['variables']['settings']['vars']['items'][0]['type']
    $form['variables']['settings']['vars']['items'][0]['label']['#default_value'] = 'node';
    $form['variables']['settings']['vars']['items'][0]['label']['#disabled'] = TRUE;
    $form['variables']['settings']['vars']['items'][0]['name']['#default_value'] = 'node';
    $form['variables']['settings']['vars']['items'][0]['name']['#disabled'] = TRUE;

    // Hide the rule elements stuff for now.
    foreach (array(
      'elements',
      'negate',
    ) as $key) {
      $form[$key]['#access'] = FALSE;
    }
    foreach (array(
      'active',
      'weight',
    ) as $key) {
      $form['settings'][$key]['#access'] = FALSE;
    }
    unset($form['variables']['negate']);
    $form_state['rules_link_temp_component'] =& $rules_link_temp_component;
    $form['entity_link']['#states'] = array(
      // Disable this form if additional variables were added.
      'disabled' => array(
        'input[name="settings[vars][items][1][name]"]' => array(
          'filled' => TRUE,
        ),
      ),
      'unchecked' => array(
        'input[name="settings[vars][items][1][name]"]' => array(
          'filled' => TRUE,
        ),
      ),
    );
  }
  else {
    $condition_set = rules_link_load_condition_set($rules_link);
    $condition_vars = $condition_set
      ->componentVariables();
    $form['entity_link']['#disabled'] = count($condition_vars) > 1 ? TRUE : FALSE;
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save Rules Link'),
      '#weight' => 40,
    );
  }
  return $form;
}