<p>To make it work, place this code in the website main module class:</p>
<pre><code>use craft\base\Element;
use craft\events\SetElementTableAttributeHtmlEvent;
use yii\base\Event;
use craft\helpers\Html;

</code></pre>
<pre><code>        Event::on(
            Element::class,
            Element::EVENT_SET_TABLE_ATTRIBUTE_HTML,
            function (SetElementTableAttributeHtmlEvent $event) {

                $fieldHandle = 'someField';
                $field = Craft::$app-&gt;fields-&gt;getFieldByHandle($fieldHandle);
                if(is_null($field)){
                    return;
                }
                if(get_class($field) != 'craft\fields\Lightswitch'){
                    return;
                }
                if($event-&gt;attribute != ('field:' . $field-&gt;uid)){
                    return;
                }

                $value = $event-&gt;sender-&gt;getFieldValue($fieldHandle);
                if (!$value) {
                    $event-&gt;html = Html::tag('span', '', [
                        'class' =&gt; 'checkbox-icon unchecked',
                        'fieldname' =&gt; $field-&gt;handle
                    ]);
                }else{
                    $label = $field-&gt;onLabel ?: Craft::t('app', 'Enabled');
                    $event-&gt;html = Html::tag('span', '', [
                        'class' =&gt; 'checkbox-icon',
                        'role' =&gt; 'img',
                        'title' =&gt; $label,
                        'aria' =&gt; [
                            'label' =&gt; $label,
                        ],
                        'fieldname' =&gt; $field-&gt;handle
                    ]);
                }



            }
        );
</code></pre>

CC BY-SA 4.0
2023-10-09T10:36:27.503Z
2023-10-09T10:36:27.503Z
Piotr Pogorzelski
<p>I'm trying to make a basic plugin that intercepts Lightswitch.php and edits getTableAttributeHtml</p>
<p>Here is the current way:</p>
<pre><code>public function getTableAttributeHtml(mixed $value, ElementInterface $element): string
    {
        if (!$value) {
            return '';
        }
        
        $label = $this-&gt;onLabel ?: Craft::t('app', 'Enabled');

        return Html::tag('span', '', [
            'class' =&gt; 'checkbox-icon',
            'role' =&gt; 'img',
            'title' =&gt; $label,
            'aria' =&gt; [
                'label' =&gt; $label,
            ],
        ]);
    }
</code></pre>
<p>I want it to do something like this:</p>
<pre><code>public function getTableAttributeHtml(mixed $value, ElementInterface $element): string
    {
        if (!$value) {
            //return '';
            return Html::tag('span', '', [
                'class' =&gt; 'checkbox-icon unchecked',
                'fieldname' =&gt; $this-&gt;handle
            ]);
        }
        
        $label = $this-&gt;onLabel ?: Craft::t('app', 'Enabled');

        return Html::tag('span', '', [
            'class' =&gt; 'checkbox-icon',
            'role' =&gt; 'img',
            'title' =&gt; $label,
            'aria' =&gt; [
                'label' =&gt; $label,
            ],
            'fieldname' =&gt; $this-&gt;handle
        ]);
    }
</code></pre>
<p>Trying to make a global plugin that does this</p>

<plugin-development>
In Craft CMS CP plugin, how can I intercept the lightswitch.php getTableAttributeHtml?
# In Craft CMS CP plugin, how can I intercept the lightswitch.php getTableAttributeHtml?

(Asked by: username_0 on 2023-09-29)

I'm trying to make a basic plugin that intercepts Lightswitch.php and edits getTableAttributeHtml
Here is the current way:
public function getTableAttributeHtml(mixed $value, ElementInterface $element): string
    {
        if (!$value) {
            return '';
        }
        
        $label = $this->onLabel ?: Craft::t('app', 'Enabled');

        return Html::tag('span', '', [
            'class' => 'checkbox-icon',
            'role' => 'img',
            'title' => $label,
            'aria' => [
                'label' => $label,
            ],
        ]);
    }

I want it to do something like this:
public function getTableAttributeHtml(mixed $value, ElementInterface $element): string
    {
        if (!$value) {
            //return '';
            return Html::tag('span', '', [
                'class' => 'checkbox-icon unchecked',
                'fieldname' => $this->handle
            ]);
        }
        
        $label = $this->onLabel ?: Craft::t('app', 'Enabled');

        return Html::tag('span', '', [
            'class' => 'checkbox-icon',
            'role' => 'img',
            'title' => $label,
            'aria' => [
                'label' => $label,
            ],
            'fieldname' => $this->handle
        ]);
    }

Trying to make a global plugin that does this

**Tags:** plugin-development

---

## 1 answers

### Answer by username_1 on 2023-10-09. Score: 1

To make it work, place this code in the website main module class:
use craft\base\Element;
use craft\events\SetElementTableAttributeHtmlEvent;
use yii\base\Event;
use craft\helpers\Html;


        Event::on(
            Element::class,
            Element::EVENT_SET_TABLE_ATTRIBUTE_HTML,
            function (SetElementTableAttributeHtmlEvent $event) {

                $fieldHandle = 'someField';
                $field = Craft::$app->fields->getFieldByHandle($fieldHandle);
                if(is_null($field)){
                    return;
                }
                if(get_class($field) != 'craft\fields\Lightswitch'){
                    return;
                }
                if($event->attribute != ('field:' . $field->uid)){
                    return;
                }

                $value = $event->sender->getFieldValue($fieldHandle);
                if (!$value) {
                    $event->html = Html::tag('span', '', [
                        'class' => 'checkbox-icon unchecked',
                        'fieldname' => $field->handle
                    ]);
                }else{
                    $label = $field->onLabel ?: Craft::t('app', 'Enabled');
                    $event->html = Html::tag('span', '', [
                        'class' => 'checkbox-icon',
                        'role' => 'img',
                        'title' => $label,
                        'aria' => [
                            'label' => $label,
                        ],
                        'fieldname' => $field->handle
                    ]);
                }



            }
        );


---

