You are here

menu_language_form.inc in Menu per language - i18n menu overview 7

The form for my menu language Parts of these are from MENU core but adapted to fit my needs.

This contains form + submit + theming

File

menu_language_form.inc
View source
<?php

/**
 * @file
 * The form for my menu language
 * Parts of these are from MENU core but adapted to fit my needs.
 *
 * This contains form + submit + theming
 *
 */
function menu_language_menulanguage_form($form, &$form_state, $menu = 'main-menu', $language_mnu_links = 'nl') {
  global $menu_admin;
  $form['#attached']['css'] = array(
    drupal_get_path('module', 'menu') . '/menu.css',
  );

  /**
   * GETTING IT WITH Q BUILDER
   */
  $result = menu_language_get_menu_links_by_language($language_mnu_links, $menu);
  foreach ($result as $item) {
    $links[] = get_object_vars($item);
  }
  $tree = menu_tree_data($links);
  $node_links = array();
  menu_tree_collect_node_links($tree, $node_links);

  // We indicate that a menu administrator is running the menu access check.
  $menu_admin = TRUE;
  menu_tree_check_access($tree, $node_links);
  $menu_admin = FALSE;
  $form = array_merge($form, _menu_language_menulanguage_form($tree));
  $form['#menu'] = $menu;
  if (element_children($form)) {
    $form['actions'] = array(
      '#type' => 'actions',
    );
    $form['actions']['submit'] = array(
      '#type' => 'submit',
      '#value' => t('Save configuration'),
    );
  }
  else {
    $form['#empty_text'] = t('There are no menu links yet. <a href="@link">@link_text</a>.', array(
      '@link' => url('admin/structure/menu/manage/' . $form['#menu']['menu_name'] . '/add'),
      '@link_text' => t('Add link'),
    ));
  }
  return $form;
}

/**
 * Returns HTML table (draggable)
 *
 * @param $variables
 *   An associative array containing:
 *   - form: A render element representing the form.
 *
 * @ingroup themeable
 */
function theme_menu_language_menulanguage_form($variables) {
  $form = $variables['form'];
  drupal_add_tabledrag('menu-overview', 'match', 'parent', 'menu-plid', 'menu-plid', 'menu-mlid', TRUE, MENU_MAX_DEPTH - 1);
  drupal_add_tabledrag('menu-overview', 'order', 'sibling', 'menu-weight');
  $header = array(
    t('Menu link'),
    array(
      'data' => t('Enabled'),
      'class' => array(
        'checkbox',
      ),
    ),
    t('Weight'),
    array(
      'data' => t('Operations'),
      'colspan' => '3',
    ),
  );
  $rows = array();
  foreach (element_children($form) as $mlid) {
    if (isset($form[$mlid]['hidden'])) {
      $element =& $form[$mlid];

      // Build a list of operations.
      $operations = array();
      foreach (element_children($element['operations']) as $op) {
        $operations[] = array(
          'data' => drupal_render($element['operations'][$op]),
          'class' => array(
            'menu-operations',
          ),
        );
      }
      while (count($operations) < 2) {
        $operations[] = '';
      }

      // Add special classes to be used for tabledrag.js.
      $element['plid']['#attributes']['class'] = array(
        'menu-plid',
      );
      $element['mlid']['#attributes']['class'] = array(
        'menu-mlid',
      );
      $element['weight']['#attributes']['class'] = array(
        'menu-weight',
      );

      // Change the parent field to a hidden. This allows any value but hides the field.
      $element['plid']['#type'] = 'hidden';
      $row = array();
      $row[] = theme('indentation', array(
        'size' => $element['#item']['depth'] - 1,
      )) . drupal_render($element['title']);
      $row[] = array(
        'data' => drupal_render($element['hidden']),
        'class' => array(
          'checkbox',
          'menu-enabled',
        ),
      );
      $row[] = drupal_render($element['weight']) . drupal_render($element['plid']) . drupal_render($element['mlid']);
      $row = array_merge($row, $operations);
      $row = array_merge(array(
        'data' => $row,
      ), $element['#attributes']);
      $row['class'][] = 'draggable';
      $rows[] = $row;
    }
  }
  if (empty($rows)) {
    $rows[] = array(
      array(
        'data' => $form['#empty_text'],
        'colspan' => '7',
      ),
    );
  }

  /**
   * MY THEMING FUNC
   */
  $output = theme('menu_language_page', array(
    'menu' => $variables['form']['#menu'],
    'table' => theme('table', array(
      'header' => $header,
      'rows' => $rows,
      'attributes' => array(
        'id' => 'menu-overview',
      ),
    )),
    'remaining_form_elements' => drupal_render_children($form),
  ));
  return $output;
}

/**
 * Submit callback
 * CORE DEFAULT
 * Updates the 'weight' column for each element in our table, taking into
 * account that item's new order after the drag and drop actions have been
 * performed.
 */
function menu_language_menulanguage_form_submit($form, &$form_state) {

  // When dealing with saving menu items, the order in which these items are
  // saved is critical. If a changed child item is saved before its parent,
  // the child item could be saved with an invalid path past its immediate
  // parent. To prevent this, save items in the form in the same order they
  // are sent by $_POST, ensuring parents are saved first, then their children.
  // See http://drupal.org/node/181126#comment-632270
  $order = array_flip(array_keys($form_state['input']));

  // Get the $_POST order.
  $form = array_merge($order, $form);

  // Update our original form with the new order.
  $updated_items = array();
  $fields = array(
    'weight',
    'plid',
  );
  foreach (element_children($form) as $mlid) {
    if (isset($form[$mlid]['#item'])) {
      $element = $form[$mlid];

      // Update any fields that have changed in this menu item.
      foreach ($fields as $field) {
        if ($element[$field]['#value'] != $element[$field]['#default_value']) {
          $element['#item'][$field] = $element[$field]['#value'];
          $updated_items[$mlid] = $element['#item'];
        }
      }

      // Hidden is a special case, the value needs to be reversed.
      if ($element['hidden']['#value'] != $element['hidden']['#default_value']) {

        // Convert to integer rather than boolean due to PDO cast to string.
        $element['#item']['hidden'] = $element['hidden']['#value'] ? 0 : 1;
        $updated_items[$mlid] = $element['#item'];
      }
    }
  }

  // Save all our changed items to the database.
  foreach ($updated_items as $item) {
    $item['customized'] = 1;
    menu_link_save($item);
  }
  drupal_set_message(t('Your configuration has been saved.'));
}

/**
 * Recursive helper function for menu_overview_form().
 *
 * @param $tree
 *   The menu_tree retrieved by menu_tree_data.
 */
function _menu_language_menulanguage_form($tree) {

  /**
   * GENERIC CODE
   */
  $base_url_admin_menu_operations = 'admin/structure/menu/item/';
  $options_menu_destination = array(
    'query' => array(
      'destination' => 'admin/structure/' . arg(2),
    ),
  );
  $form =& drupal_static(__FUNCTION__, array(
    '#tree' => TRUE,
  ));
  foreach ($tree as $data) {
    $title = '';
    $item = $data['link'];

    // Don't show callbacks; these have $item['hidden'] < 0.
    if ($item && $item['hidden'] >= 0) {
      $mlid = 'mlid:' . $item['mlid'];
      $form[$mlid]['#item'] = $item;
      $form[$mlid]['#attributes'] = $item['hidden'] ? array(
        'class' => array(
          'menu-disabled',
        ),
      ) : array(
        'class' => array(
          'menu-enabled',
        ),
      );
      $form[$mlid]['title']['#markup'] = l($item['title'], $item['href'], $item['localized_options']) . ($item['hidden'] ? ' (' . t('disabled') . ')' : '');
      $form[$mlid]['hidden'] = array(
        '#type' => 'checkbox',
        '#title' => t('Enable @title menu link', array(
          '@title' => $item['title'],
        )),
        '#title_display' => 'invisible',
        '#default_value' => !$item['hidden'],
      );
      $form[$mlid]['weight'] = array(
        '#type' => 'weight',
        '#delta' => 50,
        '#default_value' => $item['weight'],
        '#title_display' => 'invisible',
        '#title' => t('Weight for @title', array(
          '@title' => $item['title'],
        )),
      );
      $form[$mlid]['mlid'] = array(
        '#type' => 'hidden',
        '#value' => $item['mlid'],
      );
      $form[$mlid]['plid'] = array(
        '#type' => 'hidden',
        '#default_value' => $item['plid'],
      );

      // Build a list of operations.

      /**
       * CHANGE THE OPTIONS FOR THE LINKS
       */
      $operations = array();
      $operations['edit'] = array(
        '#type' => 'link',
        '#title' => t('edit'),
        '#href' => $base_url_admin_menu_operations . $item['mlid'] . '/edit',
        '#options' => $options_menu_destination,
      );

      // Only items created by the menu module can be deleted.
      if ($item['module'] == 'menu' || $item['updated'] == 1) {
        $operations['delete'] = array(
          '#type' => 'link',
          '#title' => t('delete'),
          '#href' => $base_url_admin_menu_operations . $item['mlid'] . '/delete',
          '#options' => $options_menu_destination,
        );
      }
      elseif ($item['module'] == 'system' && $item['customized']) {
        $operations['reset'] = array(
          '#type' => 'link',
          '#title' => filter_xss(t('Reset')),
          '#href' => $base_url_admin_menu_operations . $item['mlid'] . '/reset',
        );
      }
      $form[$mlid]['operations'] = $operations;
    }
    if ($data['below']) {
      _menu_language_menulanguage_form($data['below']);
    }
  }
  return $form;
}

Functions

Namesort descending Description
menu_language_menulanguage_form @file The form for my menu language Parts of these are from MENU core but adapted to fit my needs.
menu_language_menulanguage_form_submit Submit callback CORE DEFAULT Updates the 'weight' column for each element in our table, taking into account that item's new order after the drag and drop actions have been performed.
theme_menu_language_menulanguage_form Returns HTML table (draggable)
_menu_language_menulanguage_form Recursive helper function for menu_overview_form().