<p>There are several way to to this, here is one of them that I preferred.</p>
<p>First, create a custom bahavior by hook into entry event define behavior, register you class, here is the hook code.</p>
<pre><code>Event::on(
            Entry::class,
            Entry::EVENT_DEFINE_BEHAVIORS,
            static function(DefineBehaviorsEvent $event) {
                $event-&gt;behaviors['anything'] = YourEntryBehaviour::class;
            }
        );
</code></pre>
<p>Here is the example of the custom behavior class</p>
<pre><code>class YourEntryBehavior extends Behavior
{
    private $_includeInSearch;
    
    public function getIncudeInSearch()
    {
        if (isset($this-&gt;_includeInSearch)) {
            return $this-&gt;_includeInSearch;
        }
        you logic to get from database
    }
    
    public function setIncludeInSearch($i)
    {
        $this-&gt;_includeInSearch = $i;
    }
    
        
}
</code></pre>
<p>Then, to save the data into database you can hook into event after save for entry and check if the include in search is set then update it.</p>
<pre><code>Event::on(
    Entry::class,
    Entry::EVENT_AFTER_SAVE,
    function (ModelEvent $event) {
        $entry = $event-&gt;sender
        if(isset($entry-&gt;getIncudeInSearch())) {
          // you function to save into database. 
       }
    }
);
</code></pre>

CC BY-SA 4.0
2022-08-31T05:42:26.350Z
Hi @aodihis

Thank you very much for your help. I will try and see if it works.
Martin Lüpold
CC BY-SA 4.0
2022-08-26T08:07:34.517Z
2022-08-26T08:07:34.517Z
aodihis
<p>I'm trying to show a custom lightswitch field inside the right panel on the edit page of an entry and saving its state to the entry when the user clicks on save.</p>
<p>My form template looks like this:</p>
<pre><code>
&lt;div class=&quot;field lightswitch-field&quot;&gt;
  &lt;div class=&quot;heading&quot;&gt;
    Include in Search
  &lt;/div&gt;
  &lt;div class=&quot;input&quot;&gt;
    {{ forms.lightSwitch({
      id: 'includeInSearch',
      name: 'includeInSearch',
      on: true}) }}
  &lt;/div&gt;
&lt;/div&gt;
</code></pre>
<p>I achieved to show this template underneath the general entry settings with the help of the template hook <code>cp.entries.edit.settings</code> in the init function of my custom plugin.</p>
<p><a href="https://i.sstatic.net/kRpLX.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/kRpLX.png" alt="Additional Lightswitch Field underneath the entry settings" /></a></p>
<p>Where I am stuck now is how can I save the state of the lightswitch to the entry when clicking on save and get the current state to set the lightswitch state when reloading the edit page?</p>
<p>Thanks in advance
Martin</p>

<templating><field><hook><custom>
How to add Field to Template hook cp.entries.edit.settings and save value to entry?
# How to add Field to Template hook cp.entries.edit.settings and save value to entry?

(Asked by: username_0 on 2022-08-17)

I'm trying to show a custom lightswitch field inside the right panel on the edit page of an entry and saving its state to the entry when the user clicks on save.
My form template looks like this:


  
    Include in Search
  
  
    {{ forms.lightSwitch({
      id: 'includeInSearch',
      name: 'includeInSearch',
      on: true}) }}
  


I achieved to show this template underneath the general entry settings with the help of the template hook cp.entries.edit.settings in the init function of my custom plugin.

Where I am stuck now is how can I save the state of the lightswitch to the entry when clicking on save and get the current state to set the lightswitch state when reloading the edit page?
Thanks in advance
Martin

**Tags:** templating, field, hook, custom


---

## 1 answers

### Answer by username_1 on 2022-08-26. Score: 1

There are several way to to this, here is one of them that I preferred.
First, create a custom bahavior by hook into entry event define behavior, register you class, here is the hook code.
Event::on(
            Entry::class,
            Entry::EVENT_DEFINE_BEHAVIORS,
            static function(DefineBehaviorsEvent $event) {
                $event->behaviors['anything'] = YourEntryBehaviour::class;
            }
        );

Here is the example of the custom behavior class
class YourEntryBehavior extends Behavior
{
    private $_includeInSearch;
    
    public function getIncudeInSearch()
    {
        if (isset($this->_includeInSearch)) {
            return $this->_includeInSearch;
        }
        you logic to get from database
    }
    
    public function setIncludeInSearch($i)
    {
        $this->_includeInSearch = $i;
    }
    
        
}

Then, to save the data into database you can hook into event after save for entry and check if the include in search is set then update it.
Event::on(
    Entry::class,
    Entry::EVENT_AFTER_SAVE,
    function (ModelEvent $event) {
        $entry = $event->sender
        if(isset($entry->getIncudeInSearch())) {
          // you function to save into database. 
       }
    }
);

> **Comments:**
> **username_0**: Hi @username_1  Thank you very much for your help. I will try and see if it works.  

---

