Support for Drupal 7 is ending on 5 January 2025—it’s time to migrate to Drupal 10! Learn about the many benefits of Drupal 10 and find migration tools in our resource center.
By Raxter on
I am new to Drupal. I am using Drupal 7. I have inherited a custom module that uses a JSON feed to populate events on a page. The issue I am having is when the JSON feed is updated the node is not being updated with the updates. For example, if a title has been changed to an exisitn event, how can I get that to update the page? Any input would be appreciated.
$refresh_days = 1; // refresh the feed every xx days
$time = time();
$counter_new = $counter_existing = 0;
// make sure its time to run
$last_run = variable_get('gd_feed_last_run', 0);
$next_run = strtotime('+' . $refresh_days . ' day', $last_run);
// if ($next_run < $time) {
// make sure we have right category; get tid
$category_matches = taxonomy_get_term_by_name('gd', 'events');
$category = array_pop($category_matches);
if (!$category) {
watchdog('gd', 'none found', 'error');
return;
}
$feed_url = 'https://lathis/gd-forms/api/v1/yhe';
$options = array('headers' => array());
$options['headers']['Authorization'] = 'Basic ' . base64_encode('ghytt:gduyw48fjienfn39o439ihA==');
$response = drupal_http_request($feed_url, $options);
$response_data = $response->data;
$json_data = json_decode($response_data);
$feed_data = $json_data->deals;
if (!count($feed_data)) {
watchdog('gd_feed', 'bad feed request - ' . print_r($response, TRUE));
}
foreach ($feed_data as $event) {
// make sure it doesn't already exist
$event_nids = FALSE;
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'event')
->propertyCondition('title', $event->{'draftTitle'}, '=')
->fieldCondition('field_event_dates', 'value', strtotime($event->startTime) , '=')
->range(0, 1)
->addMetaData('account', user_load(1)); // Run the query as user 1.
$result = $query->execute();
if (isset($result['node'])) { // existing event found, just count it
$counter_existing++;
//assuming what I need goes here to get the updates
}
else { // event doesn't exist; create new one
//create new event - wont bore you with this create code. it works
$counter_new++;
$node = new stdClass();
$node->type = 'event';
$node->language = LANGUAGE_NONE;
node_object_prepare($node);
$node->status = 1;
$node->promote = 0;
$node->sticky = 0;
$node->comment = 0;
$node->uid = 1;
$node->title = $event->{'draftTitle'};
$node->field_event_category[$node->language][0]['tid'] = $category->tid;
$node->field_event_dates[$node->language][0]['value'] = strtotime($event->startTime);
$node->field_event_dates[$node->language][0]['value2'] = strtotime($event->endTime);
node_save($node);
}
}
variable_set('gd_feed_last_run', $time);
}