Skip to content

Conversation

@aksm
Copy link
Contributor

@aksm aksm commented Feb 22, 2023

Start on resolving #274 with refactor to the below to replace deprecated classes.

private function getTwigVariableNames(ModuleNode $nodes): array {
$variables = [];
foreach ($nodes as $node) {
if ($node instanceof \Twig_Node_Expression_Name) {
$name = $node->getAttribute('name');
$variables[$name] = $name;
}
elseif ($node instanceof \Twig_Node_Expression_Constant && $nodes instanceof \Twig_Node_Expression_GetAttr) {
$value = $node->getAttribute('value');
if (!empty($value) && is_string($value)) {
$variables[$value] = $value;
}
}
elseif ($node instanceof \Twig_Node_Expression_GetAttr) {
$path = implode('.', $this->getTwigVariableNames($node));
if (!empty($path)) {
$variables[$path] = $path;
}
}
elseif ($node instanceof \Twig_Node) {
$variables += $this->getTwigVariableNames($node);
}
}
return $variables;
}

@aksm aksm self-assigned this Feb 22, 2023
@DiegoPino
Copy link
Member

@aksm is still still on the works? Need any help or discussing?

@aksm
Copy link
Contributor Author

aksm commented Mar 20, 2023

@DiegoPino I had a question about error reporting that I can dig back up for our call tomorrow.

@aksm aksm marked this pull request as ready for review March 24, 2023 20:24
@aksm aksm requested a review from DiegoPino March 24, 2023 20:25
*/
function _format_strawberryfield_metadata_preview_error_handler($error_level, $message, $filename = NULL, $line = NULL, $context = NULL) {
_drupal_error_handler(E_WARNING, $message, $filename, $line, $context);
throw new ErrorException($message, $error_level, 0, $filename, $line);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh... smart!

$variables = [];
foreach ($nodes as $node) {
if ($node instanceof \Twig_Node_Expression_Name) {
if ($node instanceof \Twig\Node\Expression\NameExpression) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@very nice!

* A list of used $variables by this template.
*/
private function getTwigVariableNames(ModuleNode $nodes): array {
private function getTwigVariableNames(ModuleNode|Node|BodyNode $nodes): array {
Copy link
Member

Choose a reason for hiding this comment

The 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?

* AJAX callback.
*/
public static function ajaxPreview($form, FormStateInterface $form_state) {
set_error_handler('_format_strawberryfield_metadata_preview_error_handler');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cool

];
}
restore_error_handler();
restore_exception_handler();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Love this!

Copy link
Member

@DiegoPino DiegoPino left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. Let's work on the next step together. I have high dreams about an JS/Interactive JS driven JSON picker

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants