Skip to content
Snippets Groups Projects
Commit 93d5dc83 authored by Dave Reid's avatar Dave Reid
Browse files

Issue #1248710: Use the core-provided machine_name FAPI element for the...

Issue #1248710: Use the core-provided machine_name FAPI element for the importer machine name field.
parent 2e078146
No related branches found
No related tags found
No related merge requests found
......@@ -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']);
}
......
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">&nbsp;</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('');
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment