Check if a FieldHandle exists in Module without throwing 'Invalid field handle' exceptions I've been pulling my hair out trying to work this one out.
I'm building a module to auto-populate a super-table on EVENT_BEFORE_SAVE, the module works exactly the way I want it to, everything seems to be working fine.
Except Craft throws a &quot;Invalid field handle:&quot; exception if the custom field doesn't exist on that entry.
I've tried to check if the field exists with a few different methods, but anything that directly references the fieldhandle seems to throw an exception.
What's the proper way of checking if a field exists and running the code if it does?
Without modifying your code too much, you could simply add an exception handler with a try/catch block.
Another method would be to retrieve the field handles for the entry type's field layout, and check for a match:
Getting the $entryType as necessary,
$etFieldHandles = [];
$fields = $entryType-&gt;getFieldLayout()-&gt;getFields();
foreach ($fields as $field) {
    $etFieldHandles[] = $field-&gt;handle;
}

if (in_array('yourfieldhandle', $etFieldHandles) ) {
    // the field exists in this entry type's layout.
}