Custom table attributes in custom entry sources #16256
-
I'm using the EVENT_REGISTER_SOURCES event to create custom entry sources in the CP index context. $event->sources[] = [
'key' => 'exampleKey',
'sites' => [1],
'label' => Craft::t('plugin-handle', 'Source name'),
'criteria' => [
'siteId' => 1,
'section' => 'section,
'id' => $ids
],
'data' => [
'handle' => 'sourceHandle',
],
]; Is it possible to provide custom fields in the Table Attributes list for these custom sources, the fields offered are the default: Does the event offer a way to extend the ability to allow selecting custom fields required? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 6 replies
-
I think you're looking for the The former must be used together to define a virtual attribute and then provide HTML for it (here's an example of registration, and rendering). The latter is a slightly more direct version of the registration side—it allows you to act on a single element source via the dedicated use craft\events\DefineSourceTableAttributesEvent;
use craft\services\ElementSources;
use yii\base\Event;
Event::on(
ElementSources::class,
ElementSources::EVENT_DEFINE_SOURCE_TABLE_ATTRIBUTES,
function (DefineSourceTableAttributesEvent $event) {
// Skip processing for anything but entries:
if ($event->elementType !== Entry::class) {
return false;
}
// Also skip if it's not among the custom sources we are interested in:
if ($event->source !== 'exampleKey') {
return false;
}
// Add the new attribute:
$attributes = $event->attributes;
$attributes['exampleAttrKey'] = [
'label' => 'Example Attribute',
];
$event->attributes = $attributes;
}
); Note that the "key" here ( use craft\base\Element;
use craft\events\DefineAttributeHtmlEvent;
use yii\base\Event;
Event::on(
Element::class,
Element::EVENT_DEFINE_ATTRIBUTE_HTML,
function (DefineAttributeHtmlEvent $event) {
if ($event->attribute !== 'exampleAttrKey') {
return;
}
$event->html = 'HTML string!';
}
); If you want to skip processing any other handlers, you can also set |
Beta Was this translation helpful? Give feedback.
-
The selectable custom fields are based on which field layouts the element type says are available to the source. That’s defined by I’ve fixed that for the next Craft 4 and 5 releases (3a34393 + 00a6feb). So going forward, if a source key is passed to It might still make sense for your module/plugin to add a use craft\elements\Entry;
use craft\events\RegisterElementFieldLayoutsEvent;
use craft\models\EntryType;
use yii\base\Event;
Event::on(
Entry::class,
Entry::EVENT_REGISTER_FIELD_LAYOUTS,
function(RegisterElementFieldLayoutsEvent $event) {
if ($event->source === 'exampleKey') {
$section = Craft::$app->entries->getSectionByHandle('section');
$event->fieldLayouts = array_map(
fn(EntryType $entryType) => $entryType->getFieldLayout(),
$section->getEntryTypes(),
);
}
},
); (You could add that additional event handler now and custom fields will start showing up as expected; no need to wait for the next release.) |
Beta Was this translation helpful? Give feedback.
The selectable custom fields are based on which field layouts the element type says are available to the source. That’s defined by
Element::defineFieldLayouts()
. The base implementation incraft\base\Element
will return all field layouts associated with the element type, however the method is overridden byAsset
,Category
, andEntry
, in such a way that didn’t consider that the source key could have been defined viaEVENT_REGISTER_SOURCES
. So when an unexpected source key was passed, an empty array was returned rather than falling back to the default behavior.I’ve fixed that for the next Craft 4 and 5 releases (3a34393 + 00a6feb). So going forward, if a source key is passed to
defineField…