3 namespace Drupal\search_api_synonym\Plugin\search_api_synonym\import;
5 use Drupal\Core\Form\FormStateInterface;
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;
14 * Import of JSON files.
16 * @SearchApiSynonymImport(
18 * label = @Translation("JSON"),
19 * description = @Translation("Synonym import plugin from JSON file.")
22 class JSON extends ImportPluginBase implements ImportPluginInterface {
27 public function parseFile(File $file, array $settings = []) {
29 $json = file_get_contents($file->getFileUri());
31 if ($items = SerializationJSON::decode($json)) {
32 foreach ($items as $item) {
33 if (!empty($item['word']) && !empty($item['synonym'])) {
35 'word' => $item['word'],
36 'synonym' => $item['synonym'],
37 'type' => !empty($item['type']) ? $item['type'] : ''
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';
53 '#title' => $this->t('Example'),
54 '#markup' => Link::fromTextAndUrl(t('Download example file'), Url::fromUri($example_url))->toString()
62 public function validateConfigurationForm(array &$form, FormStateInterface $form_state) {
68 public function allowedExtensions() {