View source
<?php
namespace Drupal\social_demo;
use Drupal\block_content\Entity\BlockContent;
use Drupal\Core\Asset\CssOptimizer;
use Drupal\Core\Config\ConfigFactory;
use Drupal\Core\Entity\EntityStorageInterface;
use Drupal\Core\Field\FieldItemList;
use Drupal\file\Entity\File;
use Drupal\file\FileStorageInterface;
use Drupal\social_font\Entity\Font;
use Symfony\Component\DependencyInjection\ContainerInterface;
abstract class DemoSystem extends DemoContent {
protected $blockStorage;
protected $configFactory;
protected $fileStorage;
public function __construct(array $configuration, $plugin_id, $plugin_definition, DemoContentParserInterface $parser, EntityStorageInterface $block_storage, ConfigFactory $config_factory, FileStorageInterface $file_storage) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->parser = $parser;
$this->blockStorage = $block_storage;
$this->configFactory = $config_factory;
$this->fileStorage = $file_storage;
}
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('block_content'), $container
->get('config.factory'), $container
->get('entity.manager')
->getStorage('file'));
}
public function createContent() {
$data = $this
->fetchData();
$active_theme = \Drupal::theme()
->getActiveTheme()
->getName();
if (isset($data['site'])) {
$this->content['site'] = TRUE;
$config = $this->configFactory
->getEditable('system.site');
$config
->set('name', $data['site']['name'])
->save();
}
if (isset($data['homepage'])) {
$this->content['homepage'] = TRUE;
$block = $this->blockStorage
->loadByProperties([
'uuid' => '8bb9d4bb-f182-4afc-b138-8a4b802824e4',
]);
$block = current($block);
if ($block instanceof BlockContent) {
$this
->replaceAnBlock($block, $data['homepage']);
}
}
if (isset($data['theme'])) {
$this->content['theme'] = TRUE;
$config = $this->configFactory
->getEditable($active_theme . '.settings');
if (isset($data['theme']['favicon'])) {
$favicon = [
'mimetype' => $data['theme']['favicon']['mimetype'],
'path' => $data['theme']['favicon']['path'],
'url' => $data['theme']['favicon']['url'],
'use_default' => FALSE,
];
$config
->set('favicon', $favicon)
->save();
}
$logo = $this
->fetchImage($data['theme']['logo']);
if ($logo instanceof File) {
$theme_logo = [
'path' => $logo
->getFileUri(),
'url' => file_create_url($logo
->getFileUri()),
'use_default' => FALSE,
];
$config
->set('logo', $theme_logo)
->save();
}
$config
->set('font_primary', $this
->getOrCreateFont($data['theme']['font_primary']))
->save();
$config
->set('card_radius', $data['theme']['card_radius'])
->save();
$config
->set('form_control_radius', $data['theme']['form_control_radius'])
->save();
$config
->set('button_radius', $data['theme']['button_radius'])
->save();
$color = $this->configFactory
->getEditable('color.theme.' . $active_theme);
$palette = [
'brand-primary' => $data['theme']['color_primary'],
'brand-secondary' => $data['theme']['color_secondary'],
'brand-accent' => $data['theme']['color_accents'],
'brand-link' => $data['theme']['color_link'],
'navbar-bg' => $data['theme']['navbar-bg'],
'navbar-text' => $data['theme']['navbar-text'],
'navbar-active-bg' => $data['theme']['navbar-active-bg'],
'navbar-active-text' => $data['theme']['navbar-active-text'],
'navbar-sec-bg' => $data['theme']['navbar-sec-bg'],
'navbar-sec-text' => $data['theme']['navbar-sec-text'],
];
$color
->set('palette', $palette)
->save();
$libraries = [
'assets/css/brand.css',
];
$id = $active_theme . '-' . substr(hash('sha256', serialize($palette) . microtime()), 0, 8);
$paths['color'] = 'public://color';
$paths['target'] = $paths['color'] . '/' . $id;
foreach ($paths as $path) {
file_prepare_directory($path, FILE_CREATE_DIRECTORY);
}
$paths['target'] = $paths['target'] . '/';
$paths['id'] = $id;
$paths['source'] = drupal_get_path('theme', $active_theme) . '/';
$paths['files'] = $paths['map'] = [];
$css = [];
foreach ($libraries as $stylesheet) {
$files = [];
if (file_exists($paths['source'] . $stylesheet)) {
$files[] = $stylesheet;
}
foreach ($files as $file) {
$css_optimizer = new CssOptimizer();
$style = $css_optimizer
->loadFile($paths['source'] . $file, FALSE);
$css_optimizer->rewriteFileURIBasePath = base_path() . dirname($paths['source'] . $file) . '/';
$style = preg_replace_callback('/url\\([\'"]?(?![a-z]+:|\\/+)([^\'")]+)[\'"]?\\)/i', [
$css_optimizer,
'rewriteFileURI',
], $style);
$style = _color_rewrite_stylesheet($active_theme, $info, $paths, $palette, $style);
$base_file = drupal_basename($file);
$css[] = $paths['target'] . $base_file;
_color_save_stylesheet($paths['target'] . $base_file, $style, $paths);
}
}
$color
->set('stylesheets', $css)
->set('files', $paths['files'])
->save();
}
return $this->content;
}
public function getEntry(array $item) {
}
protected function fetchImage($image) {
$value = NULL;
$files = $this->fileStorage
->loadByProperties([
'uuid' => $image,
]);
if ($files) {
return current($files);
}
return $value;
}
private function getOrCreateFont($fontName) {
foreach (Font::loadMultiple() as $font_entities) {
if ($fontName == $font_entities
->get('name')->value) {
return $font_entities
->id();
}
}
$font = Font::create([
'name' => $fontName,
'user_id' => 1,
'created' => \Drupal::time()
->getRequestTime(),
'field_fallback' => '0',
]);
$font
->save();
return $font
->id();
}
private function replaceAnBlock(BlockContent $block, array $data) {
$block->field_text_block = [
'value' => $data['textblock'],
'format' => 'full_html',
];
$block_image = $this
->prepareImage($data['image'], 'Anonymous front page image homepage');
$block->field_hero_image = $block_image;
$action_links = [
[
'uri' => 'internal:' . $data['cta1']['url'],
'title' => $data['cta1']['text'],
],
[
'uri' => 'internal:' . $data['cta2']['url'],
'title' => $data['cta2']['text'],
],
];
$itemList = new FieldItemList($block->field_call_to_action_link
->getFieldDefinition());
$itemList
->setValue($action_links);
$block->field_call_to_action_link = $itemList;
$block
->save();
}
}