name = Json example parser description = Simple json parser package = Feeds core = 7.x dependencies[] = feeds
<?php /** * @file * Json example parser - simple parser plugin */ /** * Implementation of hook_feeds_plugins(). * . */ function json_example_parser_feeds_plugins() { $info = array(); $info['JsonExampleParser'] = array( 'name' => 'JSON parser', 'description' => 'Parses custom JSON.', 'handler' => array( 'parent' => 'FeedsParser', // , Feeds - FeedsFetcher, FeedsParser FeedsProcessor 'class' => 'JsonExampleParser', // , 'file' => 'JsonExampleParser.inc', // 'path' => drupal_get_path('module', 'json_example_parser'), // ), ); return $info; } // Feeds function json_example_parser_enable() { //clear the cache to display in Feeds as available plugin. cache_clear_all('plugins:feeds:plugins', 'cache'); } ?>
[ { "name":"Team Fortress 2", "year":2007, "price":0 }, { "name":"Warcraft III: The Frozen Throne", "year":2003, "price":13.9 }, { "name":"Diablo III", "year":2012, "price":33 } ]
<?php /** * A JSON example parser */ class JsonExampleParser extends FeedsParser { /** * Implements FeedsParser::parse(). */ public function parse(FeedsSource $source, FeedsFetcherResult $fetcher_result) { $result = new FeedsParserResult(); // $source_config = $source->getConfigFor($this); // JSON $fetch_items = json_decode($fetcher_result->getRaw()); foreach ($fetch_items as $value) { $item = array('name' => $value->name); if ($value->year) { $item['year'] = intval($value->year); } if ($value->price) { $item['price'] = floatval($value->price); } // if ( $source_config['type'] == 'all' || ($source_config['type'] == 'free' && $item['price'] == 0) || ($source_config['type'] == 'paid' && $item['price'] > 0)) { // $result->items[] = $item; } } return $result; } /** * Implements FeedsParser::getMappingSources(). */ public function getMappingSources() { return array( 'name' => array( 'name' => t('Game name'), 'description' => t('The name of the computer game.'), ), 'year' => array( 'name' => t('Release year'), 'description' => t('Release year of the computer game.'), ), 'price' => array( 'name' => t('Game price'), 'description' => t('The cost of the computer game.'), ), ) + parent::getMappingSources(); } /** * Configuration form. * , . */ public function configForm(&$form_state) { $form = array(); $form['type'] = array( '#type' => 'select', '#title' => t('Game type'), '#description' => t('Game filter by type.'), '#options' => array( 'all' => t('All game'), 'paid' => t('Paid'), 'free' => t('Free'), ), '#default_value' => $this->config['type'], ); return $form; } /** * Define default configuration values. * , . */ public function configDefaults() { return array( 'type' => 'all', ); } /** * Define defaults. * . */ public function sourceDefaults() { return array( 'type' => $this->config['type'], ); } /** * Show configuration form for users. * . */ public function sourceForm($source_config) { $form = array(); $form['#weight'] = -10; $form['help']['#markup'] = '<div class="help"><p>' . t('Select the type of game you want to import') . ':</p></div>'; $form['type'] = array( '#type' => 'select', '#title' => t('Game type'), '#description' => t('Game filter by type.'), '#options' => array( 'all' => t('All game'), 'paid' => t('Paid'), 'free' => t('Free'), ), '#default_value' => isset($source_config['type']) ? $source_config['type'] : 'all', ); return $form; } }
Source: https://habr.com/ru/post/164707/
All Articles