You are here

function language_hierarchy_form in Language Hierarchy 7

Build the parent-child Language hierarchy form.

Parameters

$form_state: The form state.

Return value

array A form array set for theming by theme_language_hierarchy_form()

1 string reference to 'language_hierarchy_form'
language_hierarchy_menu_alter in ./language_hierarchy.module
Implements hook_menu_alter().

File

./language_hierarchy_form.inc, line 14

Code

function language_hierarchy_form($form_state) {

  // Identify that the elements in 'language_items' are a collection, to
  // prevent Form API from flattening the array when submitted.
  $form['language_items']['#tree'] = TRUE;

  // Tabledrag will take care of updating the parent_id relationship on each
  // row of our table when we drag items around, but we need to build out the
  // initial tree structure ourselves.  This means ordering our items such
  // that children items come directly after their parent items, and all items
  // are sorted by weight relative to their siblings.
  // To keep this from cluttering the actual tabledrag code, we have moved
  // this to a dedicated function.
  // Fetch the language data from the database, ordered by parent/child/weight.
  $languages = language_hierarchy_language_list();

  // Initialise checkboxes array.
  $options = array();

  // Iterate through each database result.
  foreach ($languages as $item) {

    // Create list of enabled languages and the language checkboxes options for later use.
    $options[$item->language] = '';
    if ($item->enabled) {
      $enabled[] = $item->language;
    }

    // Create a form entry for this item.
    //
    // Each entry will be an array using the the unique language code for that item as
    // the array key, and an array of table row data as the value.
    $form['language_items'][$item->language] = array(
      // We'll use a form element of type '#markup' to display the language name.
      'name' => array(
        '#markup' => $item->name,
      ),
      // Add the native name.
      'native' => array(
        '#markup' => $item->native,
      ),
      // Add the language code. We use a seperate markup code column for display as
      // the hidden one is used in the table drag.
      'code' => array(
        '#markup' => $item->language,
      ),
      // Add language direction
      'direction' => array(
        '#markup' => $item->direction == LANGUAGE_RTL ? t('Right to left') : t('Left to right'),
      ),
      // For parent/child relationships, we also need to add form items to
      // store the current item's unique id and parent item's unique id.
      //
      // We would normally use a hidden element for this, but for this example
      // we'll use a disabled textfield element called 'id' so that we can
      // display the current item's id in the table.
      //
      // Because tabledrag modifies the #value of this element, we use
      // '#default_value' instead of '#value' when defining a hidden element.
      // Also, because tabledrag modifies '#value', we cannot use a markup
      // element, which does not support the '#value' property. (Markup
      // elements use the '#markup' property instead.)
      'id' => array(
        '#type' => 'hidden',
        '#size' => 3,
        '#default_value' => $item->language,
        '#disabled' => TRUE,
      ),
      // The same information holds true for the parent id field as for the
      // item id field, described above.
      'pid' => array(
        '#type' => 'textfield',
        '#size' => 3,
        '#default_value' => $item->parent,
      ),
      // The 'weight' field will be manipulated as we move the items around in
      // the table using the tabledrag activity.  We use the 'weight' element
      // defined in Drupal's Form API.
      'weight' => array(
        '#type' => 'weight',
        '#title' => t('Weight'),
        '#default_value' => $item->weight,
        '#delta' => 10,
        '#title_display' => 'invisible',
      ),
      // We'll use a hidden form element to pass the current 'depth' of each
      // item within our parent/child tree structure to the theme function.
      // This will be used to calculate the initial amount of indentation to
      // add before displaying any child item rows.
      'depth' => array(
        '#type' => 'hidden',
        '#value' => $item->depth,
      ),
    );
  }

  // Create language enabled checkboxes items. We'll render it by row later.
  $form['enabled'] = array(
    '#type' => 'checkboxes',
    '#title' => t('Enabled languages'),
    '#title_display' => 'invisible',
    '#options' => $options,
    '#default_value' => $enabled,
  );

  // Site default language.
  $form['site_default'] = array(
    '#type' => 'radios',
    '#title' => t('Default language'),
    '#title_display' => 'invisible',
    '#options' => $options,
    '#default_value' => language_default('language'),
  );

  // Now we add our submit button, for submitting the form results.
  //
  // The 'actions' wrapper used here isn't strictly necessary for tabledrag,
  // but is included as a Form API recommended practice.
  $form['actions'] = array(
    '#type' => 'actions',
  );
  $form['actions']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save Changes'),
  );
  return $form;
}