Skip to content
Snippets Groups Projects
Commit f883bc99 authored by Mateu Aguiló Bosch's avatar Mateu Aguiló Bosch Committed by Mateu Aguiló Bosch
Browse files

Issue #3205348 by e0ipso: Improve readability in the introspection page

parent 6bfaf258
Branches
Tags 4.0.0-alpha2
No related merge requests found
......@@ -8,7 +8,7 @@
use Drupal\Core\Entity\Display\EntityViewDisplayInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\typed_entity\RepositoryManager;
use Drupal\typed_entity_example\TypedRepositories\ArticleRepository;
use Drupal\typed_entity_example\Plugin\TypedRepositories\ArticleRepository;
use Drupal\typed_entity_example\WrappedEntities\Article;
/**
......
.class-with-variants {
background-color: rgba(20, 255, 127, 0.3);
padding: 0.02rem 0.5em 1em 0.5em;
padding: 1em 0.5em;
border-radius: 3px;
}
.class-with-variants code.className {
background-color: rgba(0, 0, 0, 0.2);
padding: 2px 6px;
border-radius: 3px;
font-weight: bold;
.class-with-variants .item-list ul,
.class-with-variants .item-list ul li {
list-style: none;
margin: 0;
padding: 0;
}
.class-with-variants .item-list ul li {
margin: 0.25em 0 0.25em 1.5em;
background: rgba(0, 0, 0, 0.2);
border-radius: 2px;
padding: 0.5em 0.75em;
}
.class-with-variants h5:first-child {
margin-top: 0;
}
.php-class-info {
background-color: rgba(20, 127, 255, 0.3);
padding: 0.02rem 0.5em 1em 0.5em;
padding: 1em 0.5em;
border-radius: 3px;
}
.php-class-info code.className {
.php-class-info .class-name {
background-color: rgba(0, 0, 0, 0.2);
padding: 2px 6px;
border-radius: 3px;
font-weight: bold;
}
.php-class-info h3:first-child {
margin-top: 0;
}
.php-class-summary .comment,
.php-class-summary .keyword,
.php-class-summary .class-name,
.php-class-summary .namespace {
font-weight: bold;
opacity: 0.8;
font-family: monospace, monospace;
}
.php-class-summary pre.comment {
margin: 0;
}
.php-class-summary .namespace {
opacity: 0.55;
}
.php-class-summary .keyword {
color: darkmagenta;
}
.php-class-summary .class-name {
background-color: rgba(0, 0, 0, 0.2);
padding: 2px 6px;
border-radius: 3px;
opacity: 1;
border-bottom: 1px dotted;
}
{{ attach_library('typed_entity_ui/class_with_variants') }}
<div{{ attributes }}>
<h5>{{ 'Falback'|t }}: </h5><pre>{{ fallback }}</pre>
{% if variants %}
{% if variants is not empty %}
<h5>{{ 'Variants'|t }}: </h5>{{ variants }}
<h5>{{ 'Falback'|t }}: </h5>
{% endif %}
{{ fallback }}
</div>
{{ attach_library('typed_entity_ui/php_class_info') }}
<div{{ attributes }}>
<h3>{{ 'Class information'|t }}</h3>
<code class="className">{{ name }}</code>
<h4>{{ 'Class DocBlock'|t }}</h4>
<pre>{{ doc_comment }}</pre>
{{ name }}
{% if parent %}
<h4>{{ 'Parent'|t }}</h4>
<code class="className">{{ parent }}</code>
{{ parent }}
{% endif %}
{{ interfaces }}
</div>
{{ attach_library('typed_entity_ui/php_class_summary') }}
<div{{ attributes }}>
<pre class="comment">{{ doc }}</pre>
<div>
<span class="keyword">{{ keyword }}</span>
<span class="namespace">{{ namespace }}</span>
<abbr title="{{ class_file }}" class="class-name">{{ name }}</abbr>
</div>
</div>
......@@ -8,3 +8,7 @@ class_with_variants:
theme:
css/typed_entity_ui.class_with_variants.css: {}
php_class_summary:
css:
theme:
css/typed_entity_ui.php_class_summary.css: {}
......@@ -10,7 +10,7 @@ use Drupal\typed_entity\Annotation\ClassWithVariantsInterface;
/**
* Implements hook_theme().
*/
function typed_entity_ui_theme($existing, $type, $theme, $path) {
function typed_entity_ui_theme($existing, $type, $theme, $path): array {
return [
'php_class_info' => [
'variables' => [
......@@ -23,25 +23,34 @@ function typed_entity_ui_theme($existing, $type, $theme, $path) {
'base_class' => NULL,
],
],
'php_class_summary' => [
'variables' => [
'class_name' => NULL,
],
],
];
}
/**
* Prepares variables for environment indicator element templates.
* Prepares variables for php_class_info templates.
*
* Default template: environment-indicator.html.twig.
* Default template: php-class-info.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #title, #value, #description, #required, #attributes.
* An associative array containing the variables.
*/
function template_preprocess_php_class_info(array &$variables) {
$reflection = $variables['reflection'];
assert($reflection instanceof ReflectionClass);
$variables['name'] = $reflection->getName();
$variables['name'] = [
'#theme' => 'php_class_summary',
'#class_name' => $reflection->getName()
];
$variables['parent'] = $reflection->getParentClass()
? $reflection->getParentClass()->getName()
? [
'#theme' => 'php_class_summary',
'#class_name' => $reflection->getParentClass()->getName()
]
: '';
$variables['interfaces'] = [
'#title' => t('Interfaces'),
......@@ -51,51 +60,62 @@ function template_preprocess_php_class_info(array &$variables) {
'#type' => 'html_tag',
'#tag' => 'code',
'#value' => $interface,
'#attributes' => ['class' => ['className']],
'#attributes' => ['class' => ['class-name']],
];
}, array_keys($reflection->getInterfaces())),
];
$variables['doc_comment'] = $reflection->getDocComment();
$variables['attributes'] = ['class' => ['php-class-info']];
}
/**
* Prepares variables for environment indicator element templates.
* Prepares variables for class_with_variants templates.
*
* Default template: environment-indicator.html.twig.
* Default template: class-with-variants.html.twig.
*
* @param array $variables
* An associative array containing:
* - element: An associative array containing the properties of the element.
* Properties used: #title, #value, #description, #required, #attributes.
* An associative array containing the variables.
*/
function template_preprocess_class_with_variants(array &$variables) {
$object = $variables['object'];
if (!$object instanceof ClassWithVariantsInterface) {
return;
}
$fallback = NULL;
$variants = [];
if ($object instanceof ClassWithVariantsInterface) {
$base_class = $variables['base_class'] ?? '';
$base_class = class_exists($base_class) ? $base_class : '';
$fallback = $object->getFallback($base_class);
$variants = $object->getVariants($base_class);
$variables['fallback'] = $fallback
? [
'#type' => 'html_tag',
'#tag' => 'code',
'#value' => $fallback,
'#attributes' => ['class' => ['className']],
]
: t('- No fallback available -');
$variables['variants'] = [
}
$build_array = function (string $class_name) {
return ['#theme' => 'php_class_summary', '#class_name' => $class_name];
};
$variables['fallback'] = t('- None available -');
if (!empty($fallback)) {
$variables['fallback'] = $build_array($fallback);
}
$variables['variants'] = empty($variants)
? NULL
: [
'#theme' => 'item_list',
'#items' => array_map(function (string $variant) {
return [
'#type' => 'html_tag',
'#tag' => 'code',
'#value' => $variant,
'#attributes' => ['class' => ['className']],
];
}, $variants),
'#items' => array_map($build_array, $variants),
];
$variables['attributes'] = ['class' => ['class-with-variants']];
}
/**
* Prepares variables for php_class_summary templates.
*
* Default template: php-class-summary.html.twig.
*
* @param array $variables
* An associative array containing the variables.
*/
function template_preprocess_php_class_summary(&$variables) {
$class_name = $variables['class_name'] ?? '';
$reflection = new \ReflectionClass($class_name);
$variables['doc'] = $reflection->getDocComment();
$variables['keyword'] = $reflection->isFinal() ? 'final class ' : 'class ';
$variables['namespace'] = $reflection->getNamespaceName() . '\\';
$variables['name'] = $reflection->getShortName();
$variables['class_file'] = t('File: @filename', ['@filename' => $reflection->getFileName()]);
$variables['attributes'] = ['class' => ['php-class-summary']];
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment