You are here

Page.php in Open Social 8

File

modules/custom/social_demo/src/Plugin/DemoContent/Page.php
View source
<?php

namespace Drupal\social_demo\Plugin\DemoContent;

use Drupal\social_demo\DemoNode;
use Drupal\social_demo\DemoContentParserInterface;
use Drupal\user\UserStorageInterface;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\file\FileStorageInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;

/**
 * Page Plugin for demo content.
 *
 * @DemoContent(
 *   id = "page",
 *   label = @Translation("Basic page"),
 *   source = "content/entity/page.yml",
 *   entity_type = "node"
 * )
 */
class Page extends DemoNode {

  /**
   * The file storage.
   *
   * @var \Drupal\file\FileStorageInterface
   */
  protected $fileStorage;

  /**
   * Page constructor.
   */
  public function __construct(array $configuration, $plugin_id, $plugin_definition, DemoContentParserInterface $parser, UserStorageInterface $user_storage, EntityStorageInterface $group_storage, FileStorageInterface $file_storage) {
    parent::__construct($configuration, $plugin_id, $plugin_definition, $parser, $user_storage, $group_storage);
    $this->fileStorage = $file_storage;
  }

  /**
   * {@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'), $container
      ->get('entity.manager')
      ->getStorage('user'), $container
      ->get('entity.manager')
      ->getStorage('group'), $container
      ->get('entity.manager')
      ->getStorage('file'));
  }

  /**
   * {@inheritdoc}
   */
  protected function getEntry(array $item) {
    $entry = parent::getEntry($item);
    $entry['field_content_visibility'] = $item['field_content_visibility'];

    // Load image by uuid and set to node.
    if (!empty($item['field_page_image'])) {
      $entry['field_page_image'] = $this
        ->prepareImage($item['field_page_image']);
    }
    if (!empty($item['alias'])) {
      $entry['path'] = [
        'alias' => $item['alias'],
      ];
    }
    return $entry;
  }

  /**
   * Prepares data about an image of node.
   *
   * @param string $uuid
   *   The uuid for the image.
   *
   * @return array|null
   *   Returns an array or null.
   */
  protected function prepareImage($uuid) {
    $value = NULL;
    $files = $this->fileStorage
      ->loadByProperties([
      'uuid' => $uuid,
    ]);
    if ($files) {
      $value = [
        [
          'target_id' => current($files)
            ->id(),
        ],
      ];
    }
    return $value;
  }

}

Classes

Namesort descending Description
Page Page Plugin for demo content.