0

In my attempt to create a table that shows all of the locales for a node and contains two editable fields, I've come up with the following functions:

function language_dashboard_overview_form($form, &$form_state, $entity) {
  $form = [];
  $options = [];

  $header = [
    'language_locale' => t('Language Locale'),
    'status' => t('Status'),
    'staging_url' => t('Staging URL'),
    'start_date' => t('Start Date'),
    'end_date' => t('End Date'),
    'display_title' => t('Display Title'),
  ];

  $translations = $entity->translations->data;

  foreach($translations as $locale => $locale_data) {
    // Add the CSS for the icons.
    drupal_add_css(drupal_get_path('module','language_dashboard') . '/css/language_dashboard.css');
    // Link to edit page for locale.
    $locale_link = "<a href='/$locale/node/$entity->nid/edit?destination=node/$entity->nid/languagepublishing'>". $locale ."</a>";
    // Locale status
    $status = $locale_data['status'] == 1 ? '<div class="check-wrapper"><span class="fluke-icon fluke-icon-checkmark"></span></div>'
      : '<div class="close-wrapper"><span class="fluke-icon fluke-icon-close"></span></div>';
    // Link to item on staging site
    $uri = entity_uri('node', $entity);
    $path_locale =  drupal_get_path_alias($uri['path'], $locale);
    $full_path = 'http://staging-igcommerce.flukedev.com/' . $locale . '/' . $path_locale;
    l('<div class="search-wrapper"><span class="fluke-icon fluke-icon-search"></span></div>', $full_path, array('html' => TRUE, 'attributes'=>array('target'=>'blank')));
    $stage_url = l('<div class="search-wrapper"><span class="fluke-icon fluke-icon-search"></span></div>', $full_path, array('html' => TRUE, 'attributes'=>array('target'=>'blank')));
    // Display title
    $title = fluke_solr_fallback($locale, $entity, 'field_content_title', "value", FALSE);
    // Define start and end date text fields.
    $start_date = isset($entity->field_locale_publishing_dates[$locale][0]['value']) ? $entity->field_locale_publishing_dates[$locale][0]['value'] : '2049-01-01T00:00:00';
    //$start_date_object = new DateTime($entity->field_locale_publishing_dates[$locale][0]['value']);
    $start_date_object = new DateTime($start_date);
    $new_start_date = $start_date_object->format('n-j-Y');

    $end_date = isset($entity->field_locale_publishing_dates[$locale][0]['value2']) ? $entity->field_locale_publishing_dates[$locale][0]['value2'] : '2050-01-01T00:00:00';
    //$end_date_object = new DateTime($entity->field_locale_publishing_dates[$locale][0]['value2']);
    $end_date_object = new DateTime($end_date);
    $new_end_date = $end_date_object->format('m-d-Y');

    $form['locales'][$locale]['checkbox_' . $locale] = [
      '#type' => 'checkbox'
    ];

    $form['locales'][$locale]['language_locale'] = [
      '#markup' => $locale_link
    ];

    $form['locales'][$locale]['status'] = [
      '#markup' => $status
    ];

    $form['locales'][$locale]['staging_url'] = [
      '#markup' => $stage_url
    ];

    $form['locales'][$locale]['start_date'] = [
      '#type' => 'textfield',
      '#size' => 10,
      '#maxlength' => 10,
      '#value' => $new_start_date
    ];

    $form['locales'][$locale]['end_date'] = [
      '#type' => 'textfield',
      '#size' => 10,
      '#maxlength' => 10,
      '#value' => $new_end_date
    ];

    $form['locales'][$locale]['display_title'] = [
      '#markup' => $title
    ];

  }

  $form['submit'] = [
    '#type' => 'submit',
    '#value' => t('Submit')
  ];

  return $form;
}

/**
 * Theme function for language_dashboard_overview_form
 */
function theme_language_dashboard_overview_form($vars) {
  $output = '';
  $rows = [];
  $form = $vars['form'];
  $locale_items = $form['locales'];

  $header = [
    t('Select'),
    t('Language Locale'),
    t('Status'),
    t('Staging URL'),
    t('Start Date'),
    t('End Date'),
    t('Display Title')
  ];

  $locales = array_filter(array_keys($locale_items), function($value) {
    return substr($value, 0, 1) != '#';
  });

  foreach($locales as $locale) {
    $test = 'a';
    $row = [
      drupal_render($form['locales'][$locale]['checkbox_' . $locale]),
      drupal_render($form['locales'][$locale]['language_locale']),
      drupal_render($form['locales'][$locale]['status']),
      drupal_render($form['locales'][$locale]['staging_url']),
      drupal_render($form['locales'][$locale]['start_date']),
      drupal_render($form['locales'][$locale]['end_date']),
      drupal_render($form['locales'][$locale]['display_title'])
    ];

    $rows[] = $row;
  }

  $table_vars = [
    'header' => $header,
    'rows' => $rows
  ];

  $output = theme('table', $table_vars);

  $output .= drupal_render($form['submit']);
  $output .= drupal_render_children($form);

  return $output;
}

/**
 * Implements hook_entity_update().
 */
function fluke_staging_entity_update($entity, $type) {
  // Index the entity to the staging environment.
  if (in_array($type, array('node', 'taxonomy_term'))) {
    fluke_staging_index_solr($entity, $type, 'staging');
  }
}

This builds the form ok, but I have two big problems when the form is submitted: 1) $form_state['values'] doesn't contain multiple records; instead it just contains values for all of the checkboxes, the two text fields, and the form values (token, id , etc) 2) The date values don't contain the updated values input into the form.

What do I need to change to achieve my desired results of 1) getting all of the rows from the table and 2) getting the updated values from the text fields?

1 Answer 1

0

1) By default $form_state['values'] holds a flattened array, it does not maintain the original form array structure and each locales keys will override the previous one. However, this should not affect 'checkbox_' . $localeUse precisely because you build up this key from $localeUse variable.

You can change this default behavior by using the #tree property that defaults to FALSE : $form['locales']['#tree'] = TRUE;


2) Use $form_state['values'] when available in your form builder. Values should be available when the form is rebuilt after it is submitted. You will have to override defaults explicitly, e.g. :

if (!empty($form_state['values'][$locale]['start_date'])) {
  $start_date = $form_state['values'][$locale]['start_date'];
}
else {
  // initial assignment
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.