From 93d5dc83a8fe6c7b536c387fe15e26a39c1c34ea Mon Sep 17 00:00:00 2001 From: Dave Reid <dave@davereid.net> Date: Sat, 13 Aug 2011 23:32:58 -0500 Subject: [PATCH] Issue #1248710: Use the core-provided machine_name FAPI element for the importer machine name field. --- feeds_ui/feeds_ui.admin.inc | 30 +++++++++++++++++++----------- feeds_ui/feeds_ui.js | 27 --------------------------- 2 files changed, 19 insertions(+), 38 deletions(-) diff --git a/feeds_ui/feeds_ui.admin.inc b/feeds_ui/feeds_ui.admin.inc index 325115f0..6a7e8bf6 100644 --- a/feeds_ui/feeds_ui.admin.inc +++ b/feeds_ui/feeds_ui.admin.inc @@ -150,8 +150,7 @@ function feeds_ui_overview_form_submit($form, &$form_state) { * of $from_importer. */ function feeds_ui_create_form($form, &$form_state, $from_importer = NULL) { - drupal_add_js(drupal_get_path('module', 'feeds_ui') . '/feeds_ui.js'); - $form = array(); + $form['#attached']['js'][] = drupal_get_path('module', 'feeds_ui') . '/feeds_ui.js'; $form['#from_importer'] = $from_importer; $form['name'] = array( '#type' => 'textfield', @@ -159,15 +158,14 @@ function feeds_ui_create_form($form, &$form_state, $from_importer = NULL) { '#description' => t('A natural name for this configuration. Example: RSS Feed. You can always change this name later.'), '#required' => TRUE, '#maxlength' => 128, - '#attributes' => array('class' => array('feed-name')), ); $form['id'] = array( - '#type' => 'textfield', - '#title' => t('Machine name'), - '#description' => t('A unique identifier for this configuration. Example: rss_feed. Must only contain lower case characters, numbers and underscores.'), + '#type' => 'machine_name', '#required' => TRUE, '#maxlength' => 128, - '#attributes' => array('class' => array('feed-id')), + '#machine_name' => array( + 'exists' => 'feeds_ui_importer_machine_name_exists', + ), ); $form['description'] = array( '#type' => 'textfield', @@ -181,15 +179,25 @@ function feeds_ui_create_form($form, &$form_state, $from_importer = NULL) { return $form; } +/** + * Validation callback for the importer machine name field. + */ +function feeds_ui_importer_machine_name_exists($id) { + if ($id == 'create') { + // Create is a reserved path for the add importer form. + return TRUE; + } + ctools_include('export'); + if (ctools_export_load_object('feeds_importer', 'conditions', array('id' => $id))) { + return TRUE; + } +} + /** * Validation handler for feeds_build_create_form(). */ function feeds_ui_create_form_validate($form, &$form_state) { - ctools_include('export'); $importer = feeds_importer($form_state['values']['id']); - if (ctools_export_load_object('feeds_importer', 'conditions', array('id' => $form_state['values']['id']))) { - form_set_error('id', t('Id is taken.')); - } $importer->configFormValidate($form_state['values']); } diff --git a/feeds_ui/feeds_ui.js b/feeds_ui/feeds_ui.js index b1c634a8..f5e8b7b1 100644 --- a/feeds_ui/feeds_ui.js +++ b/feeds_ui/feeds_ui.js @@ -1,33 +1,6 @@ Drupal.behaviors.feeds = function() { - // Export form machine-readable JS - $('.feed-name:not(.processed)').each(function() { - $('.feed-name') - .addClass('processed') - .after(' <small class="feed-id-suffix"> </small>'); - if ($('.feed-id').val() === $('.feed-name').val().toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/_+/g, '_') || $('.feed-id').val() === '') { - $('.feed-id').parents('.form-item').hide(); - $('.feed-name').keyup(function() { - var machine = $(this).val().toLowerCase().replace(/[^a-z0-9]+/g, '_').replace(/_+/g, '_'); - if (machine !== '_' && machine !== '') { - $('.feed-id').val(machine); - $('.feed-id-suffix').empty().append(' Machine name: ' + machine + ' [').append($('<a href="#">'+ Drupal.t('Edit') +'</a>').click(function() { - $('.feed-id').parents('.form-item').show(); - $('.feed-id-suffix').hide(); - $('.feed-name').unbind('keyup'); - return false; - })).append(']'); - } - else { - $('.feed-id').val(machine); - $('.feed-id-suffix').text(''); - } - }); - $('.feed-name').keyup(); - } - }); - // Hide text in specific input fields. $('.hide-text-on-focus').focus(function() { $(this).val(''); -- GitLab