-
Notifications
You must be signed in to change notification settings - Fork 9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
ISSUE-274: Report render array output error in Metadata Display preview #275
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,8 @@ | |
|
||
use Drupal\Core\Cache\Cache; | ||
use Twig\Node\ModuleNode; | ||
use Twig\Node\BodyNode; | ||
use Twig\Node\Node; | ||
use Drupal\Core\Entity\EntityStorageInterface; | ||
use Drupal\Core\Field\BaseFieldDefinition; | ||
use Drupal\Core\Entity\ContentEntityBase; | ||
|
@@ -402,6 +404,7 @@ public function renderNative(array $context) { | |
$twigtemplate, | ||
$context | ||
); | ||
|
||
return $rendered; | ||
} | ||
|
||
|
@@ -429,29 +432,35 @@ public function getTwigVariablesUsed() { | |
* @param \Twig\Node\ModuleNode $nodes | ||
* A Twig Module Nodes object. | ||
* | ||
* @param \Twig\Node\BodyNode $nodes | ||
* A Twig Module Nodes object. | ||
* | ||
* @param \Twig\Node\Node $nodes | ||
* A Twig Module Nodes object. | ||
* | ||
* @return array | ||
* A list of used $variables by this template. | ||
*/ | ||
private function getTwigVariableNames(ModuleNode $nodes): array { | ||
private function getTwigVariableNames(ModuleNode|Node|BodyNode $nodes): array { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ohhh.. this is new to me! So cool. So 3 params in the docs but really a single multi type one in the function argument? |
||
$variables = []; | ||
foreach ($nodes as $node) { | ||
if ($node instanceof \Twig_Node_Expression_Name) { | ||
if ($node instanceof \Twig\Node\Expression\NameExpression) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @very nice! |
||
$name = $node->getAttribute('name'); | ||
$variables[$name] = $name; | ||
} | ||
elseif ($node instanceof \Twig_Node_Expression_Constant && $nodes instanceof \Twig_Node_Expression_GetAttr) { | ||
elseif ($node instanceof \Twig\Node\Expression\ConstantExpression && $nodes instanceof \Twig\Node\Expression\GetAttrExpression) { | ||
$value = $node->getAttribute('value'); | ||
if (!empty($value) && is_string($value)) { | ||
$variables[$value] = $value; | ||
} | ||
} | ||
elseif ($node instanceof \Twig_Node_Expression_GetAttr) { | ||
elseif ($node instanceof \Twig\Node\Expression\GetAttrExpression) { | ||
$path = implode('.', $this->getTwigVariableNames($node)); | ||
if (!empty($path)) { | ||
$variables[$path] = $path; | ||
} | ||
} | ||
elseif ($node instanceof \Twig_Node) { | ||
elseif ($node instanceof \Twig\Node\Node) { | ||
$variables += $this->getTwigVariableNames($node); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -178,11 +178,13 @@ public function save(array $form, FormStateInterface $form_state) { | |
* AJAX callback. | ||
*/ | ||
public static function ajaxPreview($form, FormStateInterface $form_state) { | ||
set_error_handler('_format_strawberryfield_metadata_preview_error_handler'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Cool |
||
$response = new AjaxResponse(); | ||
|
||
/** @var \Drupal\format_strawberryfield\MetadataDisplayInterface $entity */ | ||
$entity = $form_state->getFormObject()->getEntity(); | ||
|
||
$used_vars = $entity->getTwigVariablesUsed(); | ||
// Attach the library necessary for using the OpenOffCanvasDialogCommand and | ||
// set the attachments for this Ajax response. | ||
$form['#attached']['library'][] = 'core/drupal.dialog.off_canvas'; | ||
|
@@ -252,6 +254,31 @@ public static function ajaxPreview($form, FormStateInterface $form_state) { | |
'mode' => 'application/json', | ||
], | ||
]; | ||
sort($used_vars); | ||
$json_keys = array_keys($jsondata); | ||
$data_json = array_map(function($key) { | ||
return 'data.' . $key; | ||
}, $json_keys); | ||
$unused_vars = array_diff($data_json,$used_vars); | ||
sort($unused_vars); | ||
$used_rows = array_map(function($used) { | ||
return [$used]; | ||
}, $used_vars); | ||
$unused_rows = array_map(function($unused) { | ||
return [$unused]; | ||
}, $unused_vars); | ||
$var_table = [ | ||
'#type' => 'table', | ||
'#header' => [t('Used Variables')], | ||
'#rows' => $used_rows, | ||
'#empty' => t('No content has been found.'), | ||
]; | ||
$json_table = [ | ||
'#type' => 'table', | ||
'#header' => [t('Unused JSON keys')], | ||
'#rows' => $unused_rows, | ||
'#empty' => t('No content has been found.'), | ||
]; | ||
|
||
// Try to Ensure we're using the twig from user's input instead of the entity's | ||
// default. | ||
|
@@ -337,6 +364,22 @@ public static function ajaxPreview($form, FormStateInterface $form_state) { | |
], | ||
]; | ||
} | ||
$output['twig_vars'] = [ | ||
'#type' => 'details', | ||
'#open' => FALSE, | ||
'#title' => 'Twig Variables', | ||
'render' => [ | ||
'table' => $var_table | ||
], | ||
]; | ||
$output['json_unused'] = [ | ||
'#type' => 'details', | ||
'#open' => FALSE, | ||
'#title' => 'Unused JSON keys', | ||
'render' => [ | ||
'table' => $json_table | ||
], | ||
]; | ||
} catch (\Exception $exception) { | ||
// Make the Message easier to read for the end user | ||
if ($exception instanceof TwigError) { | ||
|
@@ -354,7 +397,25 @@ public static function ajaxPreview($form, FormStateInterface $form_state) { | |
'#markup' => $message, | ||
] | ||
]; | ||
$output['twig_vars'] = [ | ||
'#type' => 'details', | ||
'#open' => FALSE, | ||
'#title' => 'Twig Variables', | ||
'render' => [ | ||
'table' => $var_table | ||
], | ||
]; | ||
$output['json_unused'] = [ | ||
'#type' => 'details', | ||
'#open' => FALSE, | ||
'#title' => 'Unused JSON keys', | ||
'render' => [ | ||
'table' => $json_table | ||
], | ||
]; | ||
} | ||
restore_error_handler(); | ||
restore_exception_handler(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Love this! |
||
$response->addCommand(new OpenOffCanvasDialogCommand(t('Preview'), $output, ['width' => '50%'])); | ||
} | ||
// Always refresh the Preview Element too. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh... smart!