Jump to main content Jump to doc navigation

Event: OnChunkFormRender

Fires during the rendering of the Chunk form. Use it to insert HTML straight into the form.

  • Service: 1 - Parser Service Events
  • Group: Chunks

Event Parameters

Name Description
mode Either new or upd (modSystemEvent::MODE_NEW / MODE_UPD).
id The ID of the Chunk. This will be 0 for new Chunks.
chunk A reference to the modChunk object. null for new Chunks.

Examples

Set content and save it

<?php
$eventName = $modx->event->name;
switch($eventName) {
    case 'OnChunkFormRender':
        //if we update the existing
        if ($mode == modSystemEvent::MODE_UPD) {
            //added chunk content
            $chunk->setContent('<p>Chunk content</p>');
            //you can immediately save new content
            $chunk->save();
        }
        break;
}

Set content only when it is empty

<?php
$eventName = $modx->event->name;
switch($eventName) {
    case 'OnChunkFormRender':
        //if we update the existing
        if ($mode == modSystemEvent::MODE_UPD) {
            //we take the content of the chunk
            $content = $chunk->getContent();
            // if there is no content, push in a new one
            if (!$content){
                $chunk->setContent('<p>New content</p>');
                //you can immediately save new content
                $chunk->save();
            }
        }
        break;
}

Change content without saving

<?php
$eventName = $modx->event->name;
switch($eventName) {
    case 'OnChunkFormRender':
        //if we update the existing
        if ($mode == modSystemEvent::MODE_UPD) {
            //change the entire contents of the chunk
            $chunk->setContent('<p>New content</p>');
            $chunk->set('name','NewChunkName');
            $chunk->set('description','Description');
            //you can immediately save new content $chunk->save();
        }
        break;
}

See Also

Support the team building MODX with a monthly donation.

The budget raised through OpenCollective is transparent, including payouts, and any contributor can apply to be paid for their work on MODX.

Backers

  • modmore
  • STERC
  • Digital Penguin
  • Jens Wittmann – Gestaltung & Entwicklung
  • CrewMark
  • Fabian Christen
  • Sepia River Studios
  • Dannevang Digital
  • Alex
  • A. Moreno
  • Chris Fickling
  • Stéphane Jäggi
  • Murray Wood
  • Anton Tarasov
  • JT Skaggs
  • deJaya
  • Lefthandmedia
  • eydolan
  • Following Sea
  • Guido Gallenkamp
  • YJ
  • Raffy
  • Snow Creative
  • Nick Clark
  • Guest
  • Helen
  • krisznet
  • Yanni
  • Richard

Budget

$194 per month—let's make that $500!

Learn more