1

You can listen to entries.onSaveEntry or entries.onDeleteEntry events and then get or modify the entry via $event->params['entry'];.

But how would I get the entry element and its properties in the event that a user starts editing an entry in the CP? I don't (necessarily) need to modify them, simple access is ok.

As far as I can see, there's no event available for this. And if I understand correctly, only P&T can add such an entries.onBeforeCpEditEntry event (which would probably go into Entries Controller actionEditEntry function).

If you do entry edits using front-end forms this is easy, but not so from within the CP it seems. Is there anything I overlooked, or is this simply not possible and maybe worth a feature request?

-

This question is related to this one: "How to reference the current entry element in custom Control Panel templates?" but it's not quite the same. The solutions / workarounds I posted below have some drawbacks, so I'm still looking for a better solution!

2
  • What are you trying to do exactly? There may be an alternative way to approach it that doesn't require the entry. Commented Feb 10, 2015 at 16:13
  • For example to load a script for specific entry sections only (would be useful for this). Another example would be to reset a field each time an entry is opened for editing or to simply set field defaults for new entries (no entry ID exists yet).
    – carlcs
    Commented Feb 10, 2015 at 16:29

3 Answers 3

3

I've been able to modify the entry editing form by creating a plugin that will include some javascript into CP layout. Then with javascript, I use some checks to make sure that I'm in the right place before doing any adjustments.

For example, in the following code, I checked if I'm editing an entry within section 32, then the label of postDate field will be changed, and also the expiryDate field will be hidden.

public function init()
{

    if (craft()->request->isCpRequest())
    {
        $postDateLabel = JsonHelper::encode(Craft::t('Issue Date'));

        $js = <<<EOT

if (document.getElementById('entry-form')) {
    \$(function() {
        if ( \$('input[name="sectionId"]').val() == 32 ) {
            \$('#postDate-field label').html($postDateLabel);
            \$('#expiryDate-field').addClass('hidden');
        }
    });
}

EOT;

        craft()->templates->includeJs($js);

    }

}

Hope that is what you're looking for.

2
  • Thanks dralshehri, I'd actually prefer a solution to modify the entry model before being displayed with PHP on the server, but this is probably the best workaround possible. Will try how that works for my needs next week.
    – carlcs
    Commented Apr 4, 2015 at 22:05
  • I just submitted a feature request to have onBeforeEditEntry event. Your JS approach works ok for simple and small edits, but it gets really messy once you do more complex things such as having default Matrix blocks (→ Setup default Matrix blocks and default values for sub-fields?). Thanks again for your ideas!
    – carlcs
    Commented Apr 8, 2015 at 14:19
1

Depending on what you're trying to acchieve, you could do certain things with Javascript.

var entrySection = $("#crumbs").find("li:last").text();
var entryEnabled = $("#enabled-field").find(".lightswitch").hasClass("on");

// do stuff...

Make sure to only parse the DOM for CP views (use craft()->request->isCpRequest()).

1

Another workaround would be to add a hidden Field Type with your plugin, a field that does not show in the CP's entry form.

The entry element is available in a Field Type via $this->element (→ "Field Types: Accessing Contextual Data")

Just make sure to add this field to all sections you need your plugin to work with!

/**
 * Workaround to get the entry element in the CP
 * Misuse of `getInputHtml`, make it return nothing.
 *
 * @param string $name
 * @param mixed $value
 *
 * @return null
 */
public function getInputHtml($name, $value)
{
    if ($this->element && $this->element->getElementType() == ElementType::Entry)
    {
        $entrySection = $this->element->getSection()->handle;
        $entryEnabled = $this->element->getStatus() != EntryModel::DISABLED;

        // do stuff...
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.