I am using a standalone form (Attach to content type: Use standalone form) to import nodes.
And I need to programmatically change the URL submitted when import form (/import/my_feed) is submitted.

How can I do this? I do not find this into "The developer's guide to Feeds".

Thanks for your reply.

Best regards,
Michel.

Comments

twistor’s picture

Version: 7.x-2.0-alpha8 » 7.x-2.x-dev
Priority: Major » Normal
Status: Active » Postponed (maintainer needs more info)

Are you trying to change the form value when the form gets submitted?

If so, then just use a form alter.

michel.settembrino’s picture

Hi Twistor,

Thanks for your quick reply.

No, I'm not trying to change the form value when the form gets submitted.
Sometimes the URL must be programmatically and definitevely changed. So by the next CRON run, the new URL is used.

Let's take an example.

  1. Current feeds import URL is www.domain.com/getData?q=(%22BELGIUM%22%2C%22FRANCE%22)
  2. A logged in user wants data from SPAIN. He submits a custom form where he can add SPAIN.
  3. After the form is submitted the feeds import URL must be www.domain.com/getData?q=(%22BELGIUM%22%2C%22FRANCE%22%2C%22SPAIN%22)

Is that now clear enough?

twistor’s picture

Status: Postponed (maintainer needs more info) » Active

I'm still not quite sure I follow, but you can get/set the feed source URL programmically like so:

$source = feeds_source('importer_id', 0);
$config = $source->getConfig();
$config['FeedsHTTPFetcher']['source'] = 'http://newurl';
$source->setConfig($config);
$source->save();
michel.settembrino’s picture

Status: Active » Fixed

Thanks Twistor. This is exactly what I want!
It tooks me sometimes to understand that I had to replace 'importer_id' by the id of my importer but then it works.

I would suggest to add this info into "The developer's guide to Feeds".

Best regards,
Michel.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.

Cromian’s picture

Sorry for being naive ... what hook do we use to implement the following code?

$source = feeds_source('importer_id', 0);
$config = $source->getConfig();
$config['FeedsHTTPFetcher']['source'] = 'http://newurl';
$source->setConfig($config);
$source->save();
MegaChriz’s picture

@Cromian
That depends on when you want the feeds source URL to be changed.

If you want the URL to be changed just before an import starts, implement the hook hook_feeds_before_import():

/**
 * Implements hook_feeds_before_import().
 *
 * Changes the source URL for the feeds source [document the reason why].
 */
function mymodule_feeds_before_import(FeedsSource $source) {
  $config = $source->getConfig();
  $config['FeedsHTTPFetcher']['source'] = 'http://newurl';
  $source->setConfig($config);
  $source->save();
}

If you want the URL to be changed when someone submits the import form, use a form alter hook instead. In there, add a submit handler. In the form submit handler function, perform the needed changes.

/**
 * Implements hook_form_FORM_ID_alter() for form 'feeds_import_form'.
 *
 * @see mymodule_form_feeds_import_form_submit()
 */
function mymodule_form_feeds_import_form_alter(&$form, &$form_state) {
  // Check if we have the importer that we want to conditionally change the source URL from.
  if ($form['#importer_id'] == 'myimporter') {
    $form['#submit'][] = 'mymodule_form_feeds_import_form_submit';
  }
}

/**
 * Submit handler for 'feeds_import_form'.
 *
 * Changes the source URL for the feeds source [document the reason why].
 *
 * @see mymodule_form_feeds_import_form_alter()
 */
function mymodule_form_feeds_import_form_submit(&$form, &$form_state) {
  $source = feeds_source($form['#importer_id'], 0);
  $config = $source->getConfig();
  $config['FeedsHTTPFetcher']['source'] = 'http://newurl';
  $source->setConfig($config);
  $source->save();
}
Cromian’s picture

Thanks!

kopeboy’s picture

How to compose the source URL from a field of the Feed, or a field of a referenced entity?

Example:

"Website" Taxonomy term:
- Name = website1
- field_api_base_url = "https://api.website1.com/api"
- field_api_key = "abcd"
- field_account = "user1"

Feed:
- field_website (Entity Reference) = "website1"
- field_api_module (List) = "account"
=> Source URL = [field_website:field_api_base_url]?[field_api_module]=[field_website:field_account]&api_key=[field_website:field_account]
= "https://api.website1.com/api?module=account&account=user1&api_key=abcd"

🙏🏻

kopeboy’s picture

Sorry my bad, I noticed the Drupal 7 version Feed importers are not fieldable entities.

I created this issue for 8.x version here: https://www.drupal.org/project/feeds/issues/3282260

kopeboy’s picture