You are here

DemoTaxonomyTerm.php in Open Social 10.1.x

Namespace

Drupal\social_demo

File

modules/custom/social_demo/src/DemoTaxonomyTerm.php
View source
<?php

namespace Drupal\social_demo;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Drush\Log\LogLevel;

/**
 * Class DemoTaxonomyTerm.
 *
 * @package Drupal\social_demo
 */
abstract class DemoTaxonomyTerm extends DemoContent {

  /**
   * DemoTaxonomyTerm constructor.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, DemoContentParserInterface $parser) {
    parent::__construct($configuration, $plugin_id, $plugin_definition);
    $this->parser = $parser;
  }

  /**
   * {@inheritdoc}
   */
  public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
    return new static($configuration, $plugin_id, $plugin_definition, $container
      ->get('social_demo.yaml_parser'));
  }

  /**
   * {@inheritdoc}
   */
  public function createContent($generate = FALSE, $max = NULL) {
    $data = $this
      ->fetchData();
    if ($generate === TRUE) {
      $data = $this
        ->scrambleData($data, $max);
    }
    foreach ($data as $uuid => $item) {

      // Must have uuid and same key value.
      if ($uuid !== $item['uuid']) {
        drush_log(dt("Term with uuid: {$uuid} has a different uuid in content."), LogLevel::ERROR);
        continue;
      }

      // Check whether node with same uuid already exists.
      $terms = $this->entityStorage
        ->loadByProperties([
        'uuid' => $uuid,
      ]);
      if ($terms) {
        drush_log(dt("Term with uuid: {$uuid} already exists."), LogLevel::WARNING);
        continue;
      }
      $entry = $this
        ->getEntry($item);
      $entity = $this->entityStorage
        ->create($entry);
      $entity
        ->save();
      if ($entity
        ->id()) {
        $this->content[$entity
          ->id()] = $entity;
      }
    }
    return $this->content;
  }

  /**
   * {@inheritdoc}
   */
  protected function getEntry(array $item) {
    $entry = [
      'uuid' => $item['uuid'],
      'name' => $item['name'],
      'vid' => $item['vid'],
      'created' => \Drupal::time()
        ->getRequestTime(),
    ];
    return $entry;
  }

}

Classes

Namesort descending Description
DemoTaxonomyTerm Class DemoTaxonomyTerm.