Add tool to reset locked feeds in Drupal 7


In my last article we discussed about Google latest update “Google revealed that they are using Ubuntu for their self-driving car“ Today I am going to show how to reset locked feeds. Recently while working with Drupal 7 this issue occurred and after spending long time by googling found the solution, which is shared with you all here. If a feeds batch import is interrupted or dropped because of network connection, server crash or for any unknown reason, import will be left as ‘locked state’, so that the use cannot try import again.

I have followed few steps to solve this issue mentioned below-

Step 1: Go to your file destination - site/all/modules/feeds/feeds.module

Step 2: We are going to change the “feed.module” file. Replace[-] and add [+] code below in the same file -

<?php @@ -219,6 +219,10 @@ function feeds_permission() {      $perms["clear $importer->id feeds"] = array(        'title' => t('Delete items from @name feeds', array('@name' => $importer->config['name'])),      ); + $perms["unlock $importer->id feeds"] = array( + 'title' => t('Unlock imports from @name feeds', array('@name' => $importer->config['name'])), + 'description' => t('If a feed importation breaks for some reason, users with this permission can unlock them.') + );    }    return $perms;  } @@ -272,6 +276,15 @@ function feeds_menu() {      'file' => 'feeds.pages.inc',      'type' => MENU_LOCAL_TASK,    ); + $items['import/%/unlock'] = array( + 'title' => 'Unlock', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('feeds_unlock_tab_form', 1), + 'access callback' => 'feeds_access', + 'access arguments' => array('unlock', 1), + 'file' => 'feeds.pages.inc', + 'type' => MENU_LOCAL_TASK, + );    $items['import/%/template'] = array(      'page callback' => 'feeds_importer_template',      'page arguments' => array(1), @@ -300,6 +313,16 @@ function feeds_menu() {      'type' => MENU_LOCAL_TASK,      'weight' => 11,    ); + $items['node/%node/unlock'] = array( + 'title' => 'Unlock', + 'page callback' => 'drupal_get_form', + 'page arguments' => array('feeds_unlock_tab_form', NULL, 1), + 'access callback' => 'feeds_access', + 'access arguments' => array('unlock', 1), + 'file' => 'feeds.pages.inc', + 'type' => MENU_LOCAL_TASK, + 'weight' => 11, + );    // @todo Eliminate this step and thus eliminate clearing menu cache when    // manipulating importers.    foreach (feeds_importer_load_all() as $importer) { @@ -351,11 +374,12 @@ function feeds_theme() {   * The action to be performed. Possible values are:   * - import   * - clear + * - unlock   * @param $param   * Node object or FeedsImporter id.   */  function feeds_access($action, $param) { - if (!in_array($action, array('import', 'clear'))) { + if (!in_array($action, array('import', 'clear', 'unlock'))) {      // If $action is not one of the supported actions, we return access denied.      return FALSE;    } diff --git feeds.pages.inc feeds.pages.inc index 33310ec..238b0db 100644 ?>
After

Step 3: After replacing the above lines go to your file destination - site/all/modules/feeds/feeds.pages.inc. Then remove [-] and add [+] the following line given below in "feeds.pages.inc" file

<?php @@ -188,6 +188,65 @@ function feeds_delete_tab_form_submit($form, &$form_state) {  } /** + * Render a feeds unlock form. + * + * Used on both node pages and configuration pages. + * Therefore $node may be missing. + */ +function feeds_unlock_tab_form($form, &$form_state, $importer_id, $node = NULL) { + if (empty($node)) { + $source = feeds_source($importer_id); + $form['#redirect'] = 'import/' . $source->id; + } + else { + $importer_id = feeds_get_importer_id($node->type); + $source = feeds_source($importer_id, $node->nid); + $form['#redirect'] = 'node/' . $source->feed_nid; + } + // Form cannot pass on source object. + $form['#importer_id'] = $source->id; + $form['#feed_nid'] = $source->feed_nid; + $form['source_status'] = array( + '#type' => 'fieldset', + '#title' => t('Status'), + '#tree' => TRUE, + '#value' => feeds_source_status($source), + ); + if ($source->progressImporting() == FEEDS_BATCH_COMPLETE && $source->progressClearing() == FEEDS_BATCH_COMPLETE) { + $form['source_locked'] = array( + '#type' => 'markup', + '#title' => t('Not Locked'), + '#tree' => TRUE, + '#markup' => t('This importer is not locked, therefore it cannot be unlocked.'), + ); + $form['actions']['submit']['#disabled'] = TRUE; + $form['actions']['submit']['#value'] = t('Unlock (disabled)'); + } else { + $form['actions']['submit']['#value'] = t('Unlock'); + } + return $form; +} + +/** + * Form submit handler. Resets all feeds state. + */ +function feeds_unlock_tab_form_submit($form, &$form_state) { + drupal_set_message('Import Unlocked'); + $form_state['redirect'] = $form['#redirect']; + $feed_nid = empty($form['#feed_nid']) ? 0 : $form['#feed_nid']; + $importer_id = $form['#importer_id']; + + //Is there a more API-friendly way to set the state? + db_update('feeds_source') + ->condition('id', $importer_id) + ->condition('feed_nid', $feed_nid) + ->fields(array('state' => FALSE)) + ->execute(); +} + +/**   * Handle a fetcher callback.   */  function feeds_fetcher_callback($importer, $feed_nid = 0) { ?>

Step 4: Please clear cache of your drupal available in "admin/config/development/performance" path. Now upload your modified files.

Finally feed unlocked as well as imported and you will receive success message as above screenshot. Happy importing.

Keep visiting for more new articles.

Author Details :

This article is originally posted by Subramanian Jaganathan (Drupal Developer).   It was his real time experience and shared with us.  Write me to share your experience with us.

Follow Us On Facebook Open Source Web Developers by Appsntech facebook group Twitter Open Source Web Developers by Appsntech twitter group Google+ Open Source Web Developers by Appsntech Google group Linkedin Open Source Web Developers by Appsntech, LinkedIn group
Copyright @2011-2015 appsntech.com. All rights reserved. Powered By- Sanjoy Dey Productions