123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507 |
- <?php
- /**
- *
- * Implements hook_form_FORM_ID_alter().
- *
- * The field_ui_field_edit_form is used for customizing the settings of
- * a field attached to an entity.
- */
- function tripal_fields_layout_form_field_ui_field_edit_form_alter(&$form, &$form_state, $form_id) {
- // For entity fields added by Tripal Entities we don't want the
- // the end-user to change the cardinality and the required fields
- // such that record can't be saved in Chado.
- $dbs = tripal_fields_layout_get_db_names_for_published_vocabularies();
- if (in_array($form['#instance']['entity_type'], $dbs)) {
- $form['field']['cardinality']['#access'] = FALSE;
- $form['instance']['required']['#access'] = FALSE;
- }
- // TODO: don't the the maximum length be larger than the field size.
- }
- /**
- * Implements hook_form_FORM_ID_alter().
- *
- * The field_ui_display_overview_form is used for formatting the display
- * or layout of fields attached to an entity.
- */
- function tripal_fields_layout_form_field_ui_display_overview_form_alter(&$form, &$form_state, $form_id) {
- $entity_type = $form['#entity_type'];
- $bundle_name = $form['#bundle'];
- // Get the bundle record.
- $bundle = db_select('tripal_bundle', 'tb')
- ->fields('tb')
- ->condition('bundle', $bundle_name)
- ->execute()
- ->fetchObject();
- if (module_exists('ds')) {
- drupal_set_message('Tripal is not compatible with the Display Suite (ds)
- module. If you would like to use Tripal-style panels for the layout
- of your pages please disable the Display Suite module. If you
- prefer to use the Display Suite module then disable the Tripal
- Fields Layout (tripal_fields_layout) module.', 'warning');
- }
- // Add a vertical tab fieldset at the bottom of the
- $form['overview_vert_tabs'] = array(
- '#type' => 'vertical_tabs'
- );
- $form['modes']['#group'] = 'overview_vert_tabs';
- $form['modes']['#weight'] = 1000;
- $form['te_add_panels'] = array(
- '#type' => 'fieldset',
- '#title' => 'Add a Panel',
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#group' => 'overview_vert_tabs'
- );
- $form['te_add_panels']['instructions'] = array(
- '#type' => 'item',
- '#markup' => t('You may add as many panels to your page layout as
- desired. Panels can be used to organize fields into categories....')
- );
- $form['te_add_panels']['panel_name'] = array(
- '#type' => 'textfield',
- '#title' => 'Panel Name',
- '#description' => t('Please provide a computer readable name for this
- panel. The name should only contain alphanumeric values and
- underscores. It must not begin with a number.')
- );
- $form['te_add_panels']['panel_label'] = array(
- '#type' => 'textfield',
- '#title' => 'Panel Label',
- '#description' => t('Please provide a human readable label for this
- panel. This is the name that will appear to site visitors.')
- );
- $form['te_add_panels']['message'] = array(
- '#type' => 'textarea',
- '#title' => 'Empty Message',
- '#rows' => 2,
- '#description' => t('When the panel has no fields the following
- message will be shown in the form above.')
- );
- $form['te_add_panels']['add_button'] = array(
- '#type' => 'submit',
- '#value' => 'Add Panel',
- '#name' => 'add-panel-submit'
- );
-
- // Layout Panels
- $form['te_layout_panels'] = array(
- '#type' => 'fieldset',
- '#title' => 'Order Panels',
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#group' => 'overview_vert_tabs'
- );
- $form['te_layout_panels']['instructions'] = array(
- '#type' => 'item',
- '#markup' => t('Drag and drop the panel to change its order.')
- );
- $form['te_layout_panels']['panel_items']['#tree'] = TRUE;
- // Get available panels
- $result = db_query('SELECT panel_id, name, label, weight FROM {tripal_panels} ORDER BY weight ASC');
- foreach ($result as $item) {
- $form['te_layout_panels']['panel_items'][$item->panel_id] = array(
- 'name' => array(
- '#markup' => check_plain($item->name),
- ),
- 'label' => array(
- '#markup' => check_plain($item->label),
- ),
- 'weight' => array(
- '#type' => 'weight',
- '#title' => t('Weight'),
- '#default_value' => $item->weight,
- '#delta' => 50,
- '#title_display' => 'invisible',
- ),
- );
- }
- $form['te_layout_panels']['panel_items']['#theme_wrappers'] = array('tripal_fields_layout_form_draggable_panel_table');
- // 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['te_layout_panels']['add_button'] = array(
- '#type' => 'submit',
- '#value' => 'Save Panel Order',
- '#name' => 'order-panel-submit'
- );
-
- // Configure Panels
- $form['te_configure_panels'] = array(
- '#type' => 'fieldset',
- '#title' => 'Configure Panels',
- '#collapsible' => TRUE,
- '#collapsed' => TRUE,
- '#group' => 'overview_vert_tabs'
- );
- // Make sure our default panels are in the database.
- _tripal_fields_layout_check_default_field_panels($bundle);
- // Now add each panel as a region.
- $form['fields']['#regions'] = array();
- $panels = db_select('tripal_panels', 'tp')
- ->fields('tp')
- ->condition('bundle_id', $bundle->id)
- ->orderBy('weight', 'ASC')
- ->orderBy('label', 'ASC')
- ->execute();
- $panel_options = array();
- while ($panel = $panels->fetchObject()) {
- $settings = unserialize($panel->settings);
- $form['fields']['#regions'][$panel->name] = array(
- 'title' => t($panel->label),
- 'message' => t($settings['message']),
- );
- $panel_options[$panel->name] = $panel->label;
- }
- // Set the table headers to add a new 'region' column.
- $form['fields']['#header'] = array(
- t('Field'),
- t('Weight'),
- t('Parent'),
- t('Label'),
- array('data' => t('Format'), 'colspan' => 3),
- t('Region'),
- );
- // Change the region callback for each field to place each field in the
- // proper "panel" region. Also, set the default region to be the base region.
- $fields = $form['fields'];
- $default_panel = 'te_base';
- foreach (element_children($fields) as $field_name) {
- $field_instance = field_info_instance($entity_type, $field_name, $bundle_name);
- $panel_id = db_select('tripal_panel_fields', 'tpf')
- ->fields('tpf', array('panel_id'))
- ->condition('field_id', $field_instance['id'])
- ->execute()
- ->fetchField();
- if ($panel_id) {
- $default_panel = db_select('tripal_panels', 'tp')
- ->fields('tp', array('name'))
- ->condition('panel_id', $panel_id)
- ->execute()
- ->fetchField();
- }
- $form['fields'][$field_name]['#region_callback'] = 'tripal_fields_layout_field_ui_row_region';
- $form['fields'][$field_name]['region'] = array(
- '#type' => 'select',
- '#options' => $panel_options,
- '#default_value' => $default_panel,
- '#attributes' => array(
- 'class' => array('te-field-region'),
- )
- );
- $form['fields'][$field_name]['#field_instance_id'] = array(
- '#type' => 'value',
- '#value' => $field_instance['id']
- );
- }
- // Add validate and submit handlers. Rearrange the submit callbacks
- // so ours is first.
- $form['#validate'][] = 'tripal_fields_layout_field_ui_validate';
- $submit = $form['#submit'];
- $form['#submit'] = array('tripal_fields_layout_field_ui_submit');
- $form['#submit'] = array_merge($form['#submit'], $submit);
- }
- /**
- * A helper function for checking if the default panels are in the database.
- */
- function _tripal_fields_layout_check_default_field_panels($bundle) {
- // Make sure we have records for our default regions: te_base and te_hidden.
- // First check if the base region is in the database. If not, add it.
- $te_base = db_select('tripal_panels', 'tp')
- ->fields('tp')
- ->condition('name', 'te_base')
- ->execute()
- ->fetchObject();
- if (!$te_base) {
- $settings = array(
- 'message' => 'Fields that should appear at the top of each page should be placed in the base panel. These fields are always present and help orient the user by at least indicating the necessary information to uniquely identiy the content.',
- );
- db_insert('tripal_panels')
- ->fields(array(
- 'bundle_id' => $bundle->id,
- 'name' => 'te_base',
- 'label' => 'Base Content',
- 'settings' => serialize($settings),
- 'weight' => -9999999
- ))
- ->execute();
- }
- // Next check if the hidden region is in the database. If not, add it.
- $te_base = db_select('tripal_panels', 'tp')
- ->fields('tp')
- ->condition('name', 'te_disabled')
- ->execute()
- ->fetchObject();
- if (!$te_base) {
- $settings = array(
- 'message' => 'Place field here to hide them from display.',
- );
- db_insert('tripal_panels')
- ->fields(array(
- 'bundle_id' => $bundle->id,
- 'name' => 'te_disabled',
- 'label' => 'Disabled',
- 'settings' => serialize($settings),
- 'weight' => 9999999
- ))
- ->execute();
- }
- }
- /**
- * Returns the region to which a row on the Field UI page belongs.
- *
- * @param $row
- * The current row that is being rendered in the Field UI page.
- */
- function tripal_fields_layout_field_ui_row_region($row) {
- $default_panel = 'te_base';
- $panel = '';
- $field_instance_id = $row['#field_instance_id']['#value'];
- // Get panel_id
- $panel_id = db_select('tripal_panel_fields', 'tpf')
- ->fields('tpf', array('panel_id'))
- ->condition('field_id', $field_instance_id)
- ->execute()
- ->fetchField();
-
- // Get panel name
- if ($panel_id) {
- $panel = db_select('tripal_panels', 'tp')
- ->fields('tp', array('name'))
- ->condition('panel_id', $panel_id)
- ->execute()
- ->fetchField();
- }
- // First get the panel
- if (!$panel) {
- $panel = $default_panel;
- }
-
- // See if there is a record in the tripal_panel_fields for this field.
- return $panel;
- }
- /**
- * Validates the field UI form to ensure proper panel assignments.
- *
- * @param $form
- * @param $form_state
- */
- function tripal_fields_layout_field_ui_validate($form, &$form_state) {
-
- if ($form_state ['clicked_button'] ['#name'] == 'add-panel-submit') {
- // Check if a valide panel name is provided
- $name = $form_state ['values'] ['panel_name'];
- if (! $name) {
- form_set_error ( 'panel_name', t ( "Please provide a name for the new panel." ) );
- }
- else if (preg_match ( '/^\d+/', $name )) {
- form_set_error ( 'panel_name', t ( "Panel name must not begin with a number." ) );
- }
- else if (preg_match ( '/\s+/', $name )) {
- form_set_error ( 'panel_name', t ( "Panel name should only contain alphanumeric values and underscores." ) );
- }
-
- // Check if a panel label is provided
- $label = $form_state ['values'] ['panel_label'];
- if (! $label) {
- form_set_error ( 'panel_label', t ( "Please provide a label for the new panel." ) );
- }
- }
- else if ($form_state ['clicked_button'] ['#name'] == 'order-panel-submit') {
-
- }
- else if ($form_state ['clicked_button'] ['#name'] == 'op') {
-
- }
-
- }
- /**
- * Responds to a submit from the field UI form for saving panel assignments.
- *
- * @param $form
- * @param $form_state
- */
- function tripal_fields_layout_field_ui_submit($form, &$form_state) {
- // Add a new panel
- if ($form_state ['clicked_button'] ['#name'] == 'add-panel-submit') {
- $bundle_id = $form_state['build_info']['args'][1]->id;
- $name = $form_state['values']['panel_name'];
- $label = $form_state['values']['panel_label'];
- $message = $form_state['values']['message'];
- $settings = array(
- 'message' => $message,
- );
- db_insert('tripal_panels')
- ->fields(array(
- 'bundle_id' => $bundle_id,
- 'name' => $name,
- 'label' => $label,
- 'settings' => serialize($settings),
- 'weight' => 0
- ))
- ->execute();
- }
- // Order panel
- else if ($form_state ['clicked_button'] ['#name'] == 'order-panel-submit') {
- $panels = $form_state['values']['panel_items'];
- foreach ($panels AS $id => $panel) {
- db_query(
- 'UPDATE {tripal_panels} SET weight = :weight WHERE panel_id = :id',
- array(
- ':weight' => $panel['weight'],
- ':id' => $id
- )
- );
- }
- }
- // Save field regions
- else if ($form_state ['clicked_button'] ['#name'] == 'op') {
- $fields = $form_state['values']['fields'];
- foreach($fields AS $field_name => $field_data){
-
- // Get field instance id
- $field_instance_id = $form['fields'][$field_name]['#field_instance_id']['#value'];
-
- // Get region panel_id
- $region = $field_data['region'];
- $panel_id = db_select('tripal_panels', 'tp')
- ->fields('tp', array('panel_id'))
- ->condition('name', $region)
- ->execute()
- ->fetchField();
-
- // Save
- $penal_field_id = db_select('tripal_panel_fields', 'tpf')
- ->fields('tpf', array('panel_field_id'))
- ->condition('field_id', $field_instance_id)
- ->execute()
- ->fetchField();
- if ($penal_field_id) {
- db_update('tripal_panel_fields')
- ->fields(array(
- 'panel_id' => $panel_id,
- ))
- ->condition('panel_field_id', $penal_field_id)
- ->execute();
- }
- else {
- db_insert('tripal_panel_fields')
- ->fields(array(
- 'panel_id' => $panel_id,
- 'field_id' => $field_instance_id
- ))
- ->execute();
- }
- }
- }
- }
- /**
- * Theme the Panel Order Table as a draggable table
- *
- * @param unknown $variables
- * @return unknown
- */
- function theme_tripal_fields_layout_form_draggable_panel_table ($variables) {
- $element = $variables['element'];
- $rows = array();
- foreach (element_children($element) 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().
- $element[$id]['weight']['#attributes']['class'] = array('panel-item-weight');
- $element[$id]['name']['#printed'] = FALSE;
- $element[$id]['label']['#printed'] = FALSE;
- $element[$id]['weight']['#printed'] = FALSE;
- // 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 'name' column.
- drupal_render($element[$id]['name']),
- // Add our 'description' column.
- drupal_render($element[$id]['label']),
- // Add our 'weight' column.
- drupal_render($element[$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('Name'), t('Label'), 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 = 'panel-items-table';
- // We can render our tabledrag table for output.
- $output = theme('table', array(
- 'header' => $header,
- 'rows' => $rows,
- 'attributes' => array('id' => $table_id),
- ));
- // And then render any remaining form elements (such as our submit button).
- //$output .= drupal_render_children($element);
- // 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', 'panel-item-weight');
- return $output;
- }
- /**
- * Implements hook_theme().
- */
- function tripal_fields_layout_theme($existing, $type, $theme, $path) {
- return array(
- 'tripal_fields_layout_form_draggable_panel_table' => array(
- 'render element' => 'element',
- ),
- );
- }
|