3

I'm looping through products ($rawProduct) within Craft Commerce, within a plugin, and returning products which fulfill a certain criteria. I'm then returning a $products array of objects with the following:

$product = new \stdClass;
$product->url = $rawProduct->url;
$product->courseTitle = $rawProduct->relatedFieldTitle[0]['title'];
$product->courseUrl = $rawProduct->relatedFieldTitle[0]['url'];
array_push($products, $product);

This is working great. However, when the target entry of the related field is disabled, lines three and four above are returning empty. I'd like to get this information no matter whether the target entry is disabled. Any ideas?

3

Came across this same situation myself. Was able to resolve with:

$entry->entriesFieldWithDisabledEntry->status(null)->first()

| improve this answer | |
  • This sounds perfect. Nice one. – Clive Portman Feb 26 '18 at 9:46
  • Just what I was looking for. Thanks! – Cavell Blood Mar 28 '19 at 15:52
2

To pull both enabled and disabled entries from an Entries field, you can do this:

$entry->entriesField->status = 'any';
$relatedEntries = $entry->entriesField->find();
| improve this answer | |
1

Related fields will always ignore disabled entries.

Instead of disabling entries, you could add a lightswitch field to your entries to indicate whether or not they are disabled. This would preserve relations to those entries and would return all entries, regardless of the value of the lightswitch field.

When fetching entries in other circumstances, if you only wanted to show enabled entries, you would need to always need to explicitly include a condition that the lightswitch was on (=='1').

| improve this answer | |
  • Thanks Steve. I'd prefer to do this without querying the DB for the disabled entry, so if you're right, then this is what I'll have to do as a workaround. – Clive Portman Jul 27 '16 at 10:24
  • It's the easiest way to handle it. I've used a similar system on a site I've built recently and it works really well. As far as I know, it's the only way to keep relations live whilst disabling direct access to entries that are the target of those relations. – Steve Rowling Jul 27 '16 at 10:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.