Errors in documentation for events

Created on 6 January 2025, 16 days ago

Problem/Motivation

On https://project.pages.drupalcode.org/ai/developers/events/#example-1-pre...

$messages = $event->getInput()->getMessages();

missing getMessages() documentation and should not refer to $this.

🐛 Bug report
Status

Active

Version

1.0

Component

Documentation

Created by

🇩🇪Germany marcus_johansson

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @marcus_johansson
  • 🇧🇪Belgium wouters_f Leuven

    An example that I have tested (and works):

    
    <?php
    
    namespace Drupal\ai_prompt_mgmt\EventSubscriber;
    
    use Drupal\ai\Event\PreGenerateResponseEvent;
    use Symfony\Component\EventDispatcher\EventSubscriberInterface;
    
    /**
     * Check for IP.
     */
    class GlobalTokenPromptSubscriber implements EventSubscriberInterface {
    
      /**
       * {@inheritdoc}
       *
       * @return array
       *   The pre generate response event.
       */
      public static function getSubscribedEvents(): array {
        return [
          PreGenerateResponseEvent::EVENT_NAME => 'checkBeforeRequest',
        ];
      }
    
      /**
       * Change IP before sending.
       *
       * @param \Drupal\ai\Event\PreGenerateResponseEvent $event
       *   The event to check.
       */
     public function checkBeforeRequest(PreGenerateResponseEvent $event) {
        if (in_array('ai_prompt_mgmt_replace_tokens', $event->getTags())) {
          foreach ($event->getInput()->getMessages() as $key => &$message) {
            $message->setText(str_replace(['[my_token]'],  [$my_replacement], $message->getText()));
          }
        }
      }
    }
    
  • 🇩🇪Germany marcus_johansson

    Adding this to the documentation, sorry for the low-effort issue @mrdalesmith - the issue with the documentation example was discussed on Slack by me and Frederik. Thanks Frederik!

Production build 0.71.5 2024