Skip to content

update src attribute using event #647

Answered by colinodell
ghost asked this question in Q&A
Discussion options

You must be logged in to vote

The src is not stored in the attributes array - Image extends from AbstractWebResource which stores the URL in its own field. Your code should work with a simple change:

public function __invoke(DocumentParsedEvent $e)
    {
        $walker = $e->getDocument()->walker();

        while ($event = $walker->next()) {
            $node = $event->getNode();

            if (!($node instanceof Image) || !$event->isEntering()) {
                continue;
            }

            if ($node instanceof Image) {
-               $node->data['attributes']['src'] = '';
+               $node->setUrl('');
            }
        }
    }

Alternatively, if your goal is to completely remove the image, you c…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by colinodell
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
question General questions about the project or usage
2 participants
Converted from issue

This discussion was converted from issue #416 on June 12, 2021 14:02.