<p>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.</p>

<p>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.</p>

<pre class="lang-php prettyprint-override"><code>public function init()
{

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

        $js = &lt;&lt;&lt;EOT

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

EOT;

        craft()-&gt;templates-&gt;includeJs($js);

    }

}
</code></pre>

<p>Hope that is what you're looking for.</p>

CC BY-SA 3.0
2015-04-04T22:05:26.060Z
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
CC BY-SA 3.0
2015-04-08T14:19:30.467Z
I just [submitted a feature request](http://feedback.buildwithcraft.com/forums/285221-feature-requests/suggestions/7494886-raise-an-event-before-entry-is-edited-loaded-in-cp) 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?](http://craftcms.stackexchange.com/q/9080/125)). Thanks again for your ideas!
carlcs
CC BY-SA 3.0
2015-04-04T19:42:24.117Z
2015-04-04T19:42:24.117Z
mohd4482
<p>Depending on what you're trying to acchieve, you could do certain things with Javascript.</p>

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

// do stuff...
</code></pre>

<p>Make sure to only parse the DOM for CP views (use <code>craft()-&gt;request-&gt;isCpRequest()</code>).</p>

CC BY-SA 3.0
2015-02-10T12:12:36.437Z
2015-02-10T12:12:36.437Z
carlcs
<p>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.</p>

<p>The entry element is available in a Field Type via <code>$this-&gt;element</code>
(→ <a href="http://buildwithcraft.com/docs/plugins/field-types#accessing-contextual-data" rel="nofollow">"Field Types: Accessing Contextual Data"</a>)</p>

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

<pre><code>/**
 * 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-&gt;element &amp;&amp; $this-&gt;element-&gt;getElementType() == ElementType::Entry)
    {
        $entrySection = $this-&gt;element-&gt;getSection()-&gt;handle;
        $entryEnabled = $this-&gt;element-&gt;getStatus() != EntryModel::DISABLED;

        // do stuff...
    }
}
</code></pre>

CC BY-SA 3.0
2015-02-10T12:13:39.363Z
2015-02-10T16:50:27.497Z
2015-02-10T16:50:27.497Z
carlcs
<p>You can listen to <code>entries.onSaveEntry</code> or <code>entries.onDeleteEntry</code> events and then get or modify
the entry via <code>$event-&gt;params['entry'];</code>.</p>

<p>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.</p>

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

<p>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?</p>

<p>-</p>

<p><em>This question is related to this one: <a href="https://craftcms.stackexchange.com/q/4113/125">"How to reference the current entry element in custom Control Panel templates?"</a>
but it's not quite the same. The solutions / workarounds I posted below have some drawbacks, so <strong>I'm still looking for a better solution!</em></strong></p>

CC BY-SA 3.0
2015-02-10T16:13:55.623Z
What are you trying to do exactly? There may be an alternative way to approach it that doesn't require the entry.
Douglas McDonald
CC BY-SA 3.0
2015-02-10T16:29:51.560Z
For example to load a script for specific entry sections only (would be useful for [this](http://craftcms.stackexchange.com/a/7001/125)). 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
<plugin-development><control-panel><entry><entrymodel>
Get the current entry element in the CP (edit entry view)?
# Get the current entry element in the CP (edit entry view)?

(Asked by: username_0 on 2015-02-10)

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?"](https://craftcms.stackexchange.com/q/4113/125)
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!***

**Tags:** plugin-development, control-panel, entry, entrymodel

> **Comments:**
> **username_1**: What are you trying to do exactly? There may be an alternative way to approach it that doesn't require the entry.  
> **username_0**: For example to load a script for specific entry sections only (would be useful for [this](http://craftcms.stackexchange.com/a/7001/125)). 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).  

---

## 3 answers

### Answer by username_2 on 2015-04-04. Score: 3 | Accepted answer

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 = <<templates->includeJs($js);

    }

}

Hope that is what you're looking for.

> **Comments:**
> **username_0**: 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.  
> **username_0**: I just [submitted a feature request](http://feedback.buildwithcraft.com/forums/285221-feature-requests/suggestions/7494886-raise-an-event-before-entry-is-edited-loaded-in-cp) 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?](http://craftcms.stackexchange.com/q/9080/125)). Thanks again for your ideas!  

---

### Answer by username_0 on 2015-02-10. Score: 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()).


---

### Answer by username_0 on 2015-02-10. Score: 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...
    }
}


---

