moodle/moodle · error · coding_exception

Function get_blog_entry() can not be used on restored…

Error message

Function get_blog_entry() can not be used on restored events.

What it means

This is a Moodle coding_exception thrown by blog_entry_created::get_blog_entry() when the event has been restored from a backup/restore process. Restored events cannot provide the live blog_entry object (is_restored() returns true because objectid cannot be mapped back to a real entry), so the accessor refuses to return it rather than returning a stale or invalid object. Callers must not rely on the returned \\blog_entry for restored events.

Solutions

  1. Check $event->is_restored() before calling get_blog_entry() and skip or branch accordingly.
  2. Instead of get_blog_entry(), fetch the entry directly from the database using $event->objectid (blog entry id) when the event is restored.
  3. Restructure observers to rely on event properties (objectid, userid, context) rather than the accessor, so restored events degrade gracefully.
  4. If you control the restore, ensure blogs are excluded or mappings handled, since blog entries are not mapped on restore (get_objectid_mapping returns NOT_MAPPED).

Example fix

// before
$entry = $event->get_blog_entry();
process($entry);
// after
if ($event->is_restored()) {
    $entry = new blog_entry($event->objectid);
    $entry->load_associations();
} else {
    $entry = $event->get_blog_entry();
}
process($entry);
Defensive patterns

Strategy: type-guard

Validate before calling

if ($event->is_restored()) {
    // Do not call get_blog_entry(); fall back to objectid-based DB lookup.
}

Type guard

function can_access_blog_entry(\core\event\base $event): bool {
    return method_exists($event, 'get_blog_entry') && !$event->is_restored();
}

Try / catch

try {
    $entry = $event->get_blog_entry();
} catch (\coding_exception $e) {
    $entry = new blog_entry($event->objectid); // restored event fallback
}

Prevention

When it happens

Trigger: Calling ->get_blog_entry() on a blog_entry_created (or blog_entry_updated) event instance that was reconstructed during a course restore, e.g. inside an event observer or event-handler code path that processes events captured/logged from a restored context.

Common situations: Observers handling blog_entry_created that assume get_blog_entry() always works; code processing events replayed from backups; event-monitor or log-store code that re-instantiates restored events and then calls the accessor; unit tests using restored fixtures.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/32e77e4eb10fed9b. Report an issue: GitHub.

Appendix: source

Thrown at public/lib/classes/event/blog_entry_created.php:71

    /**
     * Set blog_entry object to be used by observers.
     *
     * @param \blog_entry $blogentry A reference to the active blog_entry object.
     */
    public function set_blog_entry(\blog_entry $blogentry) {
        $this->blogentry = $blogentry;
    }

    /**
     * Returns created blog_entry object for event observers.
     *
     * @throws \coding_exception
     * @return \blog_entry
     */
    public function get_blog_entry() {
        if ($this->is_restored()) {
            throw new \coding_exception('Function get_blog_entry() can not be used on restored events.');
        }
        return $this->blogentry;
    }

    /**
     * Returns localised general event name.
     *
     * @return string
     */
    public static function get_name() {
        return get_string('evententryadded', 'core_blog');
    }

    /**
     * Returns non-localised event description with id's for admin use only.
     *
     * @return string
     */

View on GitHub (pinned to 48ea8c3322)