Answers.Body: <p>You'd have to write a <a href="https://craftcms.com/docs/plugins">custom plugin</a> that added an <a href="https://craftcms.com/docs/plugins/hooks-and-events#events">event listener</a> for the  <a href="https://craftcms.com/docs/plugins/events-reference#entries-onSaveEntry"><code>entries.saveEntry</code></a> event.</p>
  
  <p>Inside the event listener, you could check if the entry being saved has a <code>true</code> value for the <code>featured</code> Lightswitch field, and if so – pull all other entries with a <code>true</code> value for that same field (in the same section), setting their <code>featured</code> attribute to <code>false</code> before re-saving them.</p>
  
  <p>Something like the below would go into your plugin's primary class:</p>
  
  <pre><code>public function init()
  {
      parent::init();
  
      craft()-&gt;on('entries.saveEntry', function (Event $event) {
  
          $entry = $event-&gt;params['entry'];
  
          if ($entry-&gt;section-&gt;handle !== 'news' || !$entry-&gt;featured) {
              return false;
          }
  
          // Get all entries from the same section with a `true` value for the Lightswitch field
          $entries = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry, array(
              'section' =&gt; 'news',
              'featured' =&gt; true,
              'id' =&gt; 'not ' . $entry-&gt;id,
              'limit' =&gt; null,
          ))-&gt;findAll();
  
          // Update all other entries' Lightswitch field value to `false`
          foreach ($entries as $entry) {
              $entry-&gt;getContent()-&gt;setAttribute('featured', false);
              craft()-&gt;entries-&gt;saveEntry($entry);
          }
  
      });
  
  }
  </code></pre>
  
  <p>If you're new to plugin development in Craft, I'd suggest looking at the <a href="https://github.com/lindseydiloreto/craft-businesslogic">Business Logic plugin template</a> or the <a href="http://pluginfactory.io/">pluginfactory.io app</a> – also it'd be a good idea to read <a href="https://craftcms.com/docs/plugins">the official docs</a> on the subject.</p>
  
  <p>If you don't want to write a plugin, you could also solve this by foregoing the <code>featured</code> field for the entries. If you added a <code>featuredNewsStory</code> Entries field to a <a href="https://craftcms.com/docs/globals">Global Set</a> instead (and limited the Entries field to 1 entry), you'd make sure that there was only ever one featured news story in the system. <em>The editiorial workflow would suffer, though</em> – publishing a new, featured news story in this manner would mean that the editor would first have to create the story, then pop into the Global Set after saving the entry to actually mark it as "featured".</p>
  
  <p>Anyway, here's how you could pull the featured news story from a global, as well as the other news stories that <em>aren't</em> featured:</p>
  
  <pre><code>{% set featuredNewsStory = yourGlobalSetHandle.featuredNewsStory.first() %}
  {% set otherNewsStories = craft.entries.section('news').id(featuredNewsStory ? 'not ' ~ featuredNewsStory.id : null) %}
  </code></pre>
  
  <p>Of course, there is another possible "workaround" here: continue with your current solution, but add another clause; making sure that the featured news story pulled is the <em>newest one</em> only:</p>
  
  <pre><code>{% set featuredNewsStory = craft.entries.section('news').featured(1).order('postDate desc').limit(1).first() %}
  {% set otherNewsStories = craft.entries.section('news').id(featuredNewsStory ? 'not ' ~ featuredNewsStory.id : null) %}
  </code></pre>
  
  <p>With the above in place, you could just communicate to your client that "if you set more than one entry to be featured, only the newest one will actually <em>display</em> as featured" or the like.</p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-21T16:14:28.763Z
Answers.Text: Thanks for the detailed reply. I don't think it is worth the time actually creating a plugin to do such a small task but I will look into it. I may explore using some other methods such as adding a radio button to do a similar task
Answers.UserDisplayName: itisconor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-21T16:21:43.223Z
Answers.Text: @itisconor I hear you, though the PHP above is literally 90% of the plugin, though ;)
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-27T10:04:17.060Z
Answers.Text: This answer helped me a lot, thank you very much. I add my 2 cents: I had problems with validation to force the update of all the featured attribute, because it failed the validation of some files fields. I solved limiting the validation to the featured attributed with $entry->getContent()->validate('featured');
Answers.UserDisplayName: Masiorama
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-21T12:51:21.503Z
Answers.LastActivityDate: 2016-01-21T15:27:57.787Z
Answers.LastEditDate: 2016-01-21T15:27:57.787Z
Answers.UserDisplayName: Mats Mikkel Rummelhoff
Answers.Body: <p>There's no way of linking the value of lightswitch fields between entries in Craft (or even within the same entry, but then a radio button field type would be more suitable).</p>
  
  <p>You could create a <a href="https://craftcms.com/docs/globals" rel="nofollow"><code>Global</code></a> to hold the featured story. This would have an <a href="https://craftcms.com/docs/entries-fields" rel="nofollow"><code>entries</code> field</a>, limited to one entry (or however many featured stories you wanted to have). You can limit the entries field to only being able to link to entries from one section if you like.</p>
  
  <p>This gives you a central place to set the featured entries, independent of the individual entries. This setting the featured entry can be done easily without needing to edit multiple entries and to try and remember which entry is currently set to be the featured one.</p>
  
  <p>If you <code>Global</code> was called <code>featuredStories</code>, and the <code>entries</code> filed called <code>selectedEntries</code>, you would get the list of <code>entryIds</code> for the featured entries like this:</p>
  
  <pre><code>{% set featuredEntryIds = featuredStories.selectedEntries.ids() | join(',') %}
  </code></pre>
  
  <p>which you can use to find the featured entry like this:</p>
  
  <pre><code>{% for entry in craft.entries.section('news').id(featuredEntryIds) %}
  </code></pre>
  
  <p>and the other news entries like this:</p>
  
  <pre><code>{% for entry in craft.entries.section('news').id('not ' ~ featuredEntryIds) %}
  </code></pre>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-21T16:18:48.787Z
Answers.Text: Cheers for getting back to me. I think I may explore looking at radio buttons here and work around it that way. For the clients usability I feel that creating a global would be too confusing but again I will test a few options to see what works best. Thanks again
Answers.UserDisplayName: itisconor
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2016-01-21T12:54:33.207Z
Answers.LastActivityDate: 2016-01-21T12:54:33.207Z
Answers.UserDisplayName: Steve Rowling
Body: <p>I am working on a news section. There is a single featured news story so the lightswitch marked with ('1') displays the latest entry as the featured story. </p>
  
  <p>Below this the rest of the entries are displayed so here I then display all other latest entries with the lightswitch marked with ('0') or in this case ('not 1').</p>
  
  <p>My problem is that if two or three entries are all marked as the featured story (or a user creates an entry and marks it as featured then repeats a few days later and doesn't uncheck the previous one) then they will not display in the below section. Is there anyway to only allow one lightswitch to be checked as ('1') at any one time.</p>
  
  <p>Featured entry:</p>
  
  <blockquote>
    <p>{% for entry in craft.entries.section('news').featured('1').limit(1) %}</p>
  </blockquote>
  
  <p>All other news entries:</p>
  
  <blockquote>
    <p>{% for entry in craft.entries.section('news').limit(4).featured('not 1') %}</p>
  </blockquote>
  
Comments.ContentLicense: CC BY-SA 3.0
Comments.CreationDate: 2016-01-21T12:58:08.220Z
Comments.Text: You could just add an entry field to your News page (restricted to one entry) and set it from there?
Comments.UserDisplayName: Jamie Wade
Tags: <lightswitch>
Title: Only allow one Lightswitch to be active at a time
ThreadText: # Only allow one Lightswitch to be active at a time
  
  (Asked by: username_0 on 2016-01-21)
  
  I am working on a news section. There is a single featured news story so the lightswitch marked with ('1') displays the latest entry as the featured story.
  
  Below this the rest of the entries are displayed so here I then display all other latest entries with the lightswitch marked with ('0') or in this case ('not 1').
  
  My problem is that if two or three entries are all marked as the featured story (or a user creates an entry and marks it as featured then repeats a few days later and doesn't uncheck the previous one) then they will not display in the below section. Is there anyway to only allow one lightswitch to be checked as ('1') at any one time.
  
  Featured entry:
  
  > {% for entry in craft.entries.section('news').featured('1').limit(1) %}
  
  All other news entries:
  
  > {% for entry in craft.entries.section('news').limit(4).featured('not 1') %}
  
  **Tags:** lightswitch
  
  > **Comments:**
  > **username_1**: You could just add an entry field to your News page (restricted to one entry) and set it from there?  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2016-01-21. Score: 6
  
  You'd have to write a [custom plugin](https://craftcms.com/docs/plugins) that added an [event listener](https://craftcms.com/docs/plugins/hooks-and-events#events) for the [`entries.saveEntry`](https://craftcms.com/docs/plugins/events-reference#entries-onSaveEntry) event.
  
  Inside the event listener, you could check if the entry being saved has a `true` value for the `featured` Lightswitch field, and if so – pull all other entries with a `true` value for that same field (in the same section), setting their `featured` attribute to `false` before re-saving them.
  
  Something like the below would go into your plugin's primary class:
  
  ```
  public function init()
  {
      parent::init();
  
      craft()->on('entries.saveEntry', function (Event $event) {
  
          $entry = $event->params['entry'];
  
          if ($entry->section->handle !== 'news' || !$entry->featured) {
              return false;
          }
  
          // Get all entries from the same section with a `true` value for the Lightswitch field
          $entries = craft()->elements->getCriteria(ElementType::Entry, array(
              'section' => 'news',
              'featured' => true,
              'id' => 'not ' . $entry->id,
              'limit' => null,
          ))->findAll();
  
          // Update all other entries' Lightswitch field value to `false`
          foreach ($entries as $entry) {
              $entry->getContent()->setAttribute('featured', false);
              craft()->entries->saveEntry($entry);
          }
  
      });
  
  }
  
  ```
  
  If you're new to plugin development in Craft, I'd suggest looking at the [Business Logic plugin template](https://github.com/lindseydiloreto/craft-businesslogic) or the [pluginfactory.io app](http://pluginfactory.io/) – also it'd be a good idea to read [the official docs](https://craftcms.com/docs/plugins) on the subject.
  
  If you don't want to write a plugin, you could also solve this by foregoing the `featured` field for the entries. If you added a `featuredNewsStory` Entries field to a [Global Set](https://craftcms.com/docs/globals) instead (and limited the Entries field to 1 entry), you'd make sure that there was only ever one featured news story in the system. *The editiorial workflow would suffer, though* – publishing a new, featured news story in this manner would mean that the editor would first have to create the story, then pop into the Global Set after saving the entry to actually mark it as "featured".
  
  Anyway, here's how you could pull the featured news story from a global, as well as the other news stories that *aren't* featured:
  
  ```
  {% set featuredNewsStory = yourGlobalSetHandle.featuredNewsStory.first() %}
  {% set otherNewsStories = craft.entries.section('news').id(featuredNewsStory ? 'not ' ~ featuredNewsStory.id : null) %}
  
  ```
  
  Of course, there is another possible "workaround" here: continue with your current solution, but add another clause; making sure that the featured news story pulled is the *newest one* only:
  
  ```
  {% set featuredNewsStory = craft.entries.section('news').featured(1).order('postDate desc').limit(1).first() %}
  {% set otherNewsStories = craft.entries.section('news').id(featuredNewsStory ? 'not ' ~ featuredNewsStory.id : null) %}
  
  ```
  
  With the above in place, you could just communicate to your client that "if you set more than one entry to be featured, only the newest one will actually *display* as featured" or the like.
  
  > **Comments:**
  > **username_0**: Thanks for the detailed reply. I don't think it is worth the time actually creating a plugin to do such a small task but I will look into it. I may explore using some other methods such as adding a radio button to do a similar task  
  > **username_2**: @username_0 I hear you, though the PHP above is literally 90% of the plugin, though ;)  
  > **username_3**: This answer helped me a lot, thank you very much. I add my 2 cents: I had problems with validation to force the update of all the featured attribute, because it failed the validation of some files fields. I solved limiting the validation to the featured attributed with $entry->getContent()->validate('featured');  
  
  ---
  
  ### Answer by username_4 on 2016-01-21. Score: 4
  
  There's no way of linking the value of lightswitch fields between entries in Craft (or even within the same entry, but then a radio button field type would be more suitable).
  
  You could create a [`Global`](https://craftcms.com/docs/globals) to hold the featured story. This would have an [`entries` field](https://craftcms.com/docs/entries-fields), limited to one entry (or however many featured stories you wanted to have). You can limit the entries field to only being able to link to entries from one section if you like.
  
  This gives you a central place to set the featured entries, independent of the individual entries. This setting the featured entry can be done easily without needing to edit multiple entries and to try and remember which entry is currently set to be the featured one.
  
  If you `Global` was called `featuredStories`, and the `entries` filed called `selectedEntries`, you would get the list of `entryIds` for the featured entries like this:
  
  ```
  {% set featuredEntryIds = featuredStories.selectedEntries.ids() | join(',') %}
  
  ```
  
  which you can use to find the featured entry like this:
  
  ```
  {% for entry in craft.entries.section('news').id(featuredEntryIds) %}
  
  ```
  
  and the other news entries like this:
  
  ```
  {% for entry in craft.entries.section('news').id('not ' ~ featuredEntryIds) %}
  
  ```
  
  > **Comments:**
  > **username_0**: Cheers for getting back to me. I think I may explore looking at radio buttons here and work around it that way. For the clients usability I feel that creating a global would be too confusing but again I will test a few options to see what works best. Thanks again  
  
  ---
  
  