You are here

function widgets_set_form in Widgets 7

Form builder; Edit an widget set name and elements order.

Parameters

$form_state: An associative array containing the current state of the form.

$set: An widget set array.

See also

widgets_set_form_submit()

widgets_set_name_validate()

1 string reference to 'widgets_set_form'
widgets_menu in ./widgets.module
Implements hook_menu().

File

./widgets.admin.inc, line 36
Administration pages for widget settings.

Code

function widgets_set_form($form, &$form_state, $set) {

  // if destination is set, save it in session so widget element add works correctly

  /*
  	if ($_GET['destination']) {
    $_SESSION['widgets']['set_form']['destination'] = $_GET['destination'];
    $query = str_replace('destination=', 'd', $_SERVER['QUERY_STRING']);
    //drupal_goto(request_path() . '?' . $query);
  }
  else {
    unset($_SESSION['widgets']['set_form']['destination']);
  }
  */
  $title = t('Edit %name set', array(
    '%name' => $set['name'],
  ));
  drupal_set_title($title, PASS_THROUGH);

  // Adjust this form for sets that must be overridden to edit.
  $editable = (bool) ($set['storage'] & WIDGETS_STORAGE_EDITABLE);
  if (!$editable && empty($form_state['input'])) {
    drupal_set_message(t('This widget set is currently being provided by a module. Click the "Override defaults" button to change its settings.'), 'warning');
  }
  $form_state['widgets_set'] = $set;
  $form['#tree'] = TRUE;
  $form['#attached']['css'][drupal_get_path('module', 'widgets') . '/widgets.admin.css'] = array();

  // Show the thumbnail preview.
  $form['preview'] = array(
    '#type' => 'item',
    '#title' => t('Preview'),
    '#markup' => theme('widgets_set_view', array(
      'set' => $set,
    )),
  );

  // Allow the name of the set to be changed, unless this set is
  // provided by a module's hook_default_widgets_sets().
  if ($set['storage'] & WIDGETS_STORAGE_MODULE) {
    $form['name'] = array(
      '#type' => 'item',
      '#title' => t('Widget set name'),
      '#markup' => $set['name'],
      '#description' => t('This widget set is being provided by %module module and may not be renamed.', array(
        '%module' => $set['module'],
      )),
    );
  }
  else {
    $form['name'] = array(
      '#type' => 'textfield',
      '#size' => '64',
      '#title' => t('Widget set name'),
      '#default_value' => $set['name'],
      '#description' => t('The name is used as the identifier for generating the widget set. Use only lowercase alphanumeric characters, underscores (_), and hyphens (-).'),
      '#element_validate' => array(
        'widgets_set_name_validate',
      ),
      '#required' => TRUE,
    );
  }

  // Build the list of existing widget elements for this widget set.
  $form['elements'] = array(
    '#theme' => 'widgets_set_elements',
  );
  foreach ($set['elements'] as $key => $element) {
    $form['elements'][$key]['#weight'] = isset($form_state['input']['elements']) ? $form_state['input']['elements'][$key]['weight'] : NULL;
    $form['elements'][$key]['label'] = array(
      '#markup' => $element['label'],
    );
    $form['elements'][$key]['summary'] = array(
      '#markup' => isset($element['summary theme']) ? theme($element['summary theme'], array(
        'data' => $element['data'],
      )) : '',
    );
    $form['elements'][$key]['weight'] = array(
      '#type' => 'weight',
      '#title' => t('Weight for @title', array(
        '@title' => $element['label'],
      )),
      '#title_display' => 'invisible',
      '#default_value' => $element['weight'],
      '#access' => $editable,
    );

    // Only attempt to display these fields for editable sets as the 'weid'
    // key is not set for sets defined in code.
    if ($editable) {
      $form['elements'][$key]['configure'] = array(
        '#type' => 'link',
        '#title' => t('edit'),
        '#href' => 'admin/structure/widgets/sets/edit/' . $set['name'] . '/elements/' . $element['weid'],
        '#access' => $editable && isset($element['form callback']),
      );
      $form['elements'][$key]['remove'] = array(
        '#type' => 'link',
        '#title' => t('delete'),
        '#href' => 'admin/structure/widgets/sets/edit/' . $set['name'] . '/elements/' . $element['weid'] . '/delete',
        '#access' => $editable,
      );
    }
  }

  // Build the new widget element addition form and add it to the element list.
  $new_element_options = array();
  foreach (widgets_element_definitions() as $element => $definition) {
    if (isset($definition['group'])) {
      $new_element_options[$definition['group']][$element] = $definition['label'];
    }
    else {
      $new_element_options['Other'][$element] = $element;
    }
  }

  /*
  foreach (widgets_element_definitions() as $element => $definition) {
    $new_element_options[$element] = check_plain($definition['label']);
  }
  */
  $form['elements']['new'] = array(
    '#tree' => FALSE,
    '#weight' => isset($form_state['input']['weight']) ? $form_state['input']['weight'] : NULL,
    '#access' => $editable,
  );
  $form['elements']['new']['new'] = array(
    '#type' => 'select',
    '#title' => t('Element'),
    '#title_display' => 'invisible',
    '#options' => $new_element_options,
    '#empty_option' => t('Select a new element'),
  );
  $form['elements']['new']['weight'] = array(
    '#type' => 'weight',
    '#title' => t('Weight for new element'),
    '#title_display' => 'invisible',
    '#default_value' => count($form['elements']) - 1,
  );
  $form['elements']['new']['add'] = array(
    '#type' => 'submit',
    '#value' => t('Add'),
    '#validate' => array(
      'widgets_set_form_add_validate',
    ),
    '#submit' => array(
      'widgets_set_form_submit',
      'widgets_set_form_add_submit',
    ),
  );

  // add data form elements
  if (!isset($set['data'])) {
    $set['data'] = array();
  }
  $form['data'] = widgets_set_form_data_fields($set['data'], $set, $editable);
  if (isset($set['form callback']) && function_exists($set['form callback'])) {
    $fields = call_user_func($set['form callback'], $set['data'], $editable);
    if (is_array($fields)) {
      $form['data'] = array_merge($form['data'], $fields);
    }
  }
  drupal_alter('widgets_set_form', $form['data'], $set);

  // Show the Override or Submit button for this set.
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['override'] = array(
    '#type' => 'submit',
    '#value' => t('Override defaults'),
    '#validate' => array(),
    '#submit' => array(
      'widgets_set_form_override_submit',
    ),
    '#access' => !$editable,
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Update set'),
    '#access' => $editable,
  );
  return $form;
}