Added the Search API Synonym module to deal specifically with licence and license...
[yaffs-website] / web / modules / contrib / search_api_synonym / src / Plugin / search_api_synonym / import / JSON.php
1 <?php
2
3 namespace Drupal\search_api_synonym\Plugin\search_api_synonym\import;
4
5 use Drupal\Core\Form\FormStateInterface;
6 use Drupal\Core\Link;
7 use Drupal\Core\Url;
8 use Drupal\Component\Serialization\Json AS SerializationJSON;
9 use Drupal\file\Entity\File;
10 use Drupal\search_api_synonym\Import\ImportPluginBase;
11 use Drupal\search_api_synonym\Import\ImportPluginInterface;
12
13 /**
14  * Import of JSON files.
15  *
16  * @SearchApiSynonymImport(
17  *   id = "json",
18  *   label = @Translation("JSON"),
19  *   description = @Translation("Synonym import plugin from JSON file.")
20  * )
21  */
22 class JSON extends ImportPluginBase implements ImportPluginInterface {
23
24   /**
25    * {@inheritdoc}
26    */
27   public function parseFile(File $file, array $settings = []) {
28     $data = [];
29     $json = file_get_contents($file->getFileUri());
30
31     if ($items = SerializationJSON::decode($json)) {
32       foreach ($items as $item) {
33         if (!empty($item['word']) && !empty($item['synonym'])) {
34           $data[] = [
35             'word' => $item['word'],
36             'synonym' => $item['synonym'],
37             'type' => !empty($item['type']) ? $item['type'] : ''
38           ];
39         }
40       }
41     }
42
43     return $data;
44   }
45
46   /**
47    * {@inheritdoc}
48    */
49   public function buildConfigurationForm(array $form, FormStateInterface $form_state) {
50     $example_url = 'internal:' . base_path() . drupal_get_path('module', 'search_api_synonym') . '/examples/example.json';
51     $form['template'] = [
52       '#type' => 'item',
53       '#title' => $this->t('Example'),
54       '#markup' => Link::fromTextAndUrl(t('Download example file'), Url::fromUri($example_url))->toString()
55     ];
56     return $form;
57   }
58
59   /**
60    * {@inheritdoc}
61    */
62   public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
63   }
64
65   /**
66    * {@inheritdoc}
67    */
68   public function allowedExtensions() {
69     return ['json'];
70   }
71
72 }