templating-8.x-2.0/src/EntityInlineTemplate.php
src/EntityInlineTemplate.php
<?php
namespace Drupal\templating;
use Drupal\Core\Url;
use Drupal\Core\Render\Markup;
use Drupal\file\Entity\File;
class EntityInlineTemplate extends BaseServiceEntityInlineTemplate
{
function getTemplateView($variables)
{
$view_name = $variables['id'];
$view_display = $variables['display_id'];
$theme = $this->is_allowed();
if (!$theme) {
return false;
}
$config_name = "view--" . $theme . '-' . trim($view_name) . '-' . trim($view_display);
$suggestion_1 = $this->formatName($config_name);
$templates_views = \Drupal::entityQuery('node')
->condition('type', 'templating')
->condition('status', '1')
->condition('title', $suggestion_1, 'STARTS_WITH')
->execute();
$results = [];
if (!empty($templates_views)) {
foreach ($templates_views as $id) {
$template = \Drupal::entityTypeManager()->getStorage('node')->load($id);
$view_section = $template->field_templating_mode_view->value;
$template_content = $template->field_templating_html->value;
$results[$view_section] = $template_content;
}
}
return $results;
}
function getTemplateEntity($entity, $view_mode)
{
$output = $this->getTemplatingDatabase($entity, $view_mode);
return $output;
}
function getTemplatingDatabase($entity, $mode_view)
{
$theme = $this->is_allowed();
if (!$theme) {
return false;
}
$entity_name = $entity->getEntityTypeId();
$bundle = $entity->bundle();
$id = $entity->id();
$output = false;
$hook_name = $this->formatName($entity_name . '--' . $theme . '-' . $bundle . "-" . $mode_view . ".html.twig");
$content = $this->getTemplatingByTitle($hook_name);
if (!is_object($content)) {
$hook_name_base = $entity_name . '--' . $theme . '-' . $bundle . "-" . $mode_view . ".html.twig";
$content_base = $this->getTemplatingByTitle($hook_name_base);
if (is_object($content_base)) {
$content = $content_base;
}
}
$hook_name_id = $entity_name . '--' . $theme . '-' . $bundle . "-" . $id . "-" . $mode_view . ".html.twig";
$content_id = $this->getTemplatingByTitle($hook_name_id);
if (is_object($content_id)) {
$content = $content_id;
}
if (is_object($content)) {
$output = $content->field_templating_html->value;
}
return $output;
}
function generateLibrary()
{
$libs = $this->getLibrary();
$output = false;
if ($libs) {
$url = Url::fromRoute('<current>');
$str_url = \Drupal::service('path_alias.manager')->getAliasByPath($url->toString());
foreach ($libs as $node) {
$current_theme = \Drupal::theme()->getActiveTheme();
$theme = $current_theme->getName();
$mytheme = $node->field_theme->value;
$type = $node->field_lib_type->value;
$position = $node->field_lib_position->value;
$paths = $node->field_lib_condition->value;
$array_paths = explode(PHP_EOL, $paths);
$array_paths = array_map(
function ($item) {
return is_string($item) ? trim($item) : $item;
},
$array_paths
);
$allowed_path = false;
if (in_array("*",$array_paths)) {
$allowed_path = true;
}
if (in_array($str_url,$array_paths)) {
$allowed_path = true;
}
if ($allowed_path && $mytheme == $theme) {
$is_external = $this->is_field_ready($node, 'field_lib_url');
$url = false;
// if external or internal
if ($is_external) {
$url = trim($node->field_lib_url->value);
} else {
$is_file = $this->is_field_ready($node, 'field_lib_file');
if ($is_file) {
$file = File::load($node->field_lib_file->target_id);
if (is_object($file)) {
$url = URl::fromUri(file_create_url($file->getFileUri()))->toString();
}
}
}
// if css or js
if ($url) {
if ($type == "css") {
$output[$type . '_' . $position] = $output[$type . '_' . $position] . '<link rel="stylesheet" href="' . $url . '" crossorigin="" />';
}
if ($type == "js") {
$output[$type . '_' . $position] = $output[$type . '_' . $position] . '<script src="' . $url . '" crossorigin=""></script>';
}
}
}
}
}
return $output;
}
function getLibrary()
{
$results = false;
$libs = \Drupal::entityQuery('node')
->condition('type', 'library')
->condition('status', '1')
->execute();
$results = [];
if (!empty($libs)) {
foreach ($libs as $id) {
$node = \Drupal::entityTypeManager()->getStorage('node')->load($id);
if($node->status && $node->status->value == 1){
$results[$id] = $node;
}
}
}
return $results;
}
function htmlPage(){
$node = \Drupal::request()->attributes->get('node');
$is_ready = $this->is_field_ready($node,'body');
if ($node && $node->bundle()=='html_page' && $is_ready ) {
$body = $node->body->value ;
$var['entity'] = $node ;
return [
'#type' => 'inline_template',
'#template' => $body,
'#context' => $var
];
}
return null;
}
}
