You are here

function theme_paragraphs_bundle_settings_form in Paragraphs 7

Theme function for paragraphs bundle settings form.

See also

paragraphs_form_field_ui_field_edit_form_alter

File

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

Code

function theme_paragraphs_bundle_settings_form($variables) {
  $form = $variables['form'];

  // Initialize the variable which will store our table rows.
  $rows = array();
  uasort($form['instance']['settings']['allowed_bundles_table'], 'element_sort');

  // Iterate over each element in our $form['example_items'] array.
  foreach (element_children($form['instance']['settings']['allowed_bundles_table']) as $id) {

    // Before we add our 'weight' column to the row, we need to give the
    // element a custom class so that it can be identified in the
    // drupal_add_tabledrag call.
    //
    // This could also have been done during the form declaration by adding
    // '#attributes' => array('class' => 'example-item-weight'),
    // directy to the 'weight' element in tabledrag_example_simple_form().
    $form['instance']['settings']['allowed_bundles_table'][$id]['weight']['#attributes']['class'] = array(
      'paragraphs-bundle-item-weight',
    );

    // We are now ready to add each element of our $form data to the $rows
    // array, so that they end up as individual table cells when rendered
    // in the final table.  We run each element through the drupal_render()
    // function to generate the final html markup for that element.
    $rows[] = array(
      'data' => array(
        // Add our 'enabled' column.
        drupal_render($form['instance']['settings']['allowed_bundles_table'][$id]['enabled']),
        // Add our 'weight' column.
        drupal_render($form['instance']['settings']['allowed_bundles_table'][$id]['weight']),
      ),
      // To support the tabledrag behaviour, we need to assign each row of the
      // table a class attribute of 'draggable'. This will add the 'draggable'
      // class to the <tr> element for that row when the final table is
      // rendered.
      'class' => array(
        'draggable',
      ),
    );
  }

  // We now define the table header values.  Ensure that the 'header' count
  // matches the final column count for your table.
  $header = array(
    t('Bundle'),
    t('Weight'),
  );

  // We also need to pass the drupal_add_tabledrag() function an id which will
  // be used to identify the <table> element containing our tabledrag form.
  // Because an element's 'id' should be unique on a page, make sure the value
  // you select is NOT the same as the form ID used in your form declaration.
  $table_id = drupal_html_id('paragraphs-bundle-table');

  // We can render our tabledrag table for output.
  $output = theme('table', array(
    'header' => $header,
    'rows' => $rows,
    'attributes' => array(
      'id' => $table_id,
    ),
  ));
  $form['instance']['settings']['allowed_bundles_table']['#markup'] = $output;

  // And then render any remaining form elements (such as our submit button).
  $output = drupal_render_children($form);

  // We now call the drupal_add_tabledrag() function in order to add the
  // tabledrag.js goodness onto our page.
  //
  // For a basic sortable table, we need to pass it:
  // - the $table_id of our <table> element,
  // - the $action to be performed on our form items ('order'),
  // - a string describing where $action should be applied ('siblings'),
  // - and the class of the element containing our 'weight' element.
  drupal_add_tabledrag($table_id, 'order', 'sibling', 'paragraphs-bundle-item-weight');
  return $output;
}