You are here

function theme_paragraphs_field_multiple_value_form in Paragraphs 7

Returns HTML for an individual form element.

Combine multiple values into a table with drag-n-drop reordering. TODO : convert to a template.

Parameters

array $variables: An associative array containing:

  • element: A render element representing the form element.

Return value

string An html string.

1 theme call to theme_paragraphs_field_multiple_value_form()
paragraphs_field_multiple_value_form in ./paragraphs.field_widget.inc
Special handling to create form elements for multiple values.

File

./paragraphs.module, line 1179
Paragraphs hooks and common functions.

Code

function theme_paragraphs_field_multiple_value_form(array $variables) {
  $element = $variables['element'];
  $output = '';
  $instance = $element['#instance'];
  if (!isset($instance['settings']['title'])) {
    $instance['settings']['title'] = PARAGRAPHS_DEFAULT_TITLE;
  }
  if (!isset($instance['settings']['title_multiple'])) {
    $instance['settings']['title_multiple'] = PARAGRAPHS_DEFAULT_TITLE_MULTIPLE;
  }
  $add_mode = isset($instance['settings']['add_mode']) ? $instance['settings']['add_mode'] : PARAGRAPHS_DEFAULT_ADD_MODE;
  $required = !empty($element['#required']) ? theme('form_required_marker', $variables) : '';

  // Sort items according to '_weight' (needed when the form comes back after
  // preview or failed validation)
  $items = array();
  foreach (element_children($element) as $key) {
    if ($key === 'add_more') {
      $add_more_button =& $element[$key];
    }
    elseif ($key === 'add_more_type') {
      $add_more_button_type =& $element[$key];
    }
    else {
      if (!isset($element[$key]['#access']) || $element[$key]['#access']) {
        $items[] =& $element[$key];
      }
    }
  }
  usort($items, '_field_sort_items_value_helper');

  // If the field can hold more than one item, display it as a draggable table.
  if ($element['#cardinality'] != 1) {
    $table_id = drupal_html_id($element['#field_name'] . '_values');
    $order_class = $element['#field_name'] . '-' . $table_id . '-delta-order';
    $header = array(
      array(
        'data' => '<label>' . t('!title !required', array(
          '!title' => $element['#title'],
          '!required' => $required,
        )) . '</label>',
        'colspan' => 2,
        'class' => array(
          'field-label',
        ),
      ),
      t('Order'),
    );
    $rows = array();

    // Add the items as table rows.
    foreach ($items as $key => $item) {
      $item['_weight']['#attributes']['class'] = array(
        $order_class,
      );
      $delta_element = drupal_render($item['_weight']);
      $cells = array(
        array(
          'data' => '',
          'class' => array(
            'field-multiple-drag',
          ),
        ),
        drupal_render($item),
        array(
          'data' => $delta_element,
          'class' => array(
            'delta-order',
          ),
        ),
      );
      $class = !empty($item['#bundle']) ? array(
        'draggable',
        drupal_html_class('paragraphs_item_type_' . $item['#bundle']),
      ) : array();
      $rows[] = array(
        'data' => $cells,
        'class' => $class,
      );
    }
    $field_content = theme('table', array(
      'header' => $header,
      'rows' => $rows,
      'attributes' => array(
        'id' => $table_id,
        'class' => array(
          'field-multiple-table',
        ),
      ),
    ));
    drupal_add_tabledrag($table_id, 'order', 'sibling', $order_class);
  }
  else {
    $value_id = drupal_html_id($element['#field_name'] . '_value');
    $field_content = '<label for="' . $value_id . '">' . t('!title !required', array(
      '!title' => $element['#title'],
      '!required' => $required,
    )) . '</label>';
    if (count($items)) {

      // We don't need to render a weight field when there can only be one item.
      unset($items[0]['_weight']);
      $field_content .= '<div id="' . $value_id . '">' . drupal_render($items[0]) . '</div>';
    }
  }
  $output = '<div class="form-item">';
  if (count($items)) {
    $output .= $field_content;
  }
  else {
    $replacements = array(
      '@title_multiple' => t('@title', array(
        '@title' => $instance['settings']['title_multiple'],
      )),
      '@title' => t('@title', array(
        '@title' => $instance['settings']['title'],
      )),
    );
    if (count($element['#select_bundles']) === 1) {
      if ($element['#cardinality'] == 1) {
        $add_text = t('The @title has not been added yet. Press the button below to add it.', $replacements);
      }
      else {
        $add_text = t('No @title_multiple added yet. Press the button below to add one.', $replacements);
      }
    }
    elseif ($add_mode == 'button') {
      $add_text = t('No @title_multiple added yet. Select a @title type and press a button below to add one.', $replacements);
    }
    else {
      $add_text = t('No @title_multiple added yet. Select a @title type and press the button below to add one.', $replacements);
    }
    $output .= '<label>' . t('!title !required', array(
      '!title' => $element['#title'],
      '!required' => $required,
    )) . "</label>";
    $output .= '<p><em>' . $add_text . '</em></p>';
  }
  $output .= $element['#description'] ? '<div class="description">' . $element['#description'] . '</div>' : '';
  $output .= '<div class="clearfix">' . drupal_render($add_more_button_type) . drupal_render($add_more_button) . '</div>';
  $output .= '</div>';
  return $output;
}