Page.php in Open Social 8
Same filename in this branch
Same filename and directory in other branches
- 8.9 modules/custom/social_demo/src/Plugin/DemoContent/Page.php
- 8.2 modules/custom/social_demo/src/Plugin/DemoContent/Page.php
- 8.3 modules/custom/social_demo/src/Plugin/DemoContent/Page.php
- 8.4 modules/custom/social_demo/src/Plugin/DemoContent/Page.php
- 8.5 modules/custom/social_demo/src/Plugin/DemoContent/Page.php
- 8.6 modules/custom/social_demo/src/Plugin/DemoContent/Page.php
- 8.7 modules/custom/social_demo/src/Plugin/DemoContent/Page.php
- 8.8 modules/custom/social_demo/src/Plugin/DemoContent/Page.php
- 10.3.x modules/custom/social_demo/src/Plugin/DemoContent/Page.php
- 10.0.x modules/custom/social_demo/src/Plugin/DemoContent/Page.php
- 10.1.x modules/custom/social_demo/src/Plugin/DemoContent/Page.php
- 10.2.x modules/custom/social_demo/src/Plugin/DemoContent/Page.php
Namespace
Drupal\social_demo\Plugin\DemoContentFile
modules/custom/social_demo/src/Plugin/DemoContent/Page.phpView 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;
}
}