You are here

protected function CronHandler::getTerms in Instagram Feeds 8

Gets IDs of existing ones or creates new taxonomy terms based on hashtags.

Parameters

\Drupal\Core\Field\FieldDefinitionInterface $field_definition: The array of field definitions for the bundle, keyed by field name.

string[] $tags: Instagram hash tags.

Return value

array An array for entity reference field with target_id => term->id().

1 call to CronHandler::getTerms()
CronHandler::createMediaEntity in src/CronHandler.php
Creates Media entity with Instagram data.

File

src/CronHandler.php, line 278

Class

CronHandler
Instagram Feeds Cron Handler Service.

Namespace

Drupal\instagram_feeds

Code

protected function getTerms(FieldDefinitionInterface $field_definition, $tags) {
  $result = [];
  $settings = $field_definition
    ->getSetting('handler_settings');

  /** @var \Drupal\taxonomy\TermStorageInterface $term_storage */
  $term_storage = $this->entityTypeManager
    ->getStorage('taxonomy_term');
  $vocabulary = $settings['auto_create_bundle'] ?: reset($settings['target_bundles']);
  $terms = $term_storage
    ->loadByProperties([
    'name' => $tags,
    'vid' => $vocabulary,
  ]);
  $existing_tags = [];
  foreach ($terms as $term) {
    $result[]['target_id'] = $term
      ->id();
    $existing_tags[] = $term
      ->label();
  }
  $create_tags = $settings['auto_create'] ? array_diff($tags, $existing_tags) : [];
  foreach ($create_tags as $tag) {

    /** @var \Drupal\taxonomy\TermInterface $term */
    $term = $term_storage
      ->create([
      'name' => $tag,
      'vid' => $vocabulary,
    ]);
    $term
      ->save();
    $result[]['target_id'] = $term
      ->id();
  }
  return $result;
}