1

In channel A, we have a Neo field with a couple of different block types, in 1 of those types we have an entries field with channel B entries.

On the channel B entry, I'd like to find out if it is related to a channel A entry through that Neo field.

Going by https://craftcms.com/docs/relations and how it works for a Matrix field I have this:

      {% set partOfChannelA = craft.entries.section('channelA').relatedTo({
        targetElement: entryChannelB,
        field: 'neoField.blockType.fieldHandle'
    }) %}

But that doesn't work :) Am I missing something or does this simply not work with a Neo field? Thanks for helping out!

  • Try to add the field id or the field object instead of this string. It's easier in my opinion and has less room for errors.. And of course it's faster – Robin Schambach Dec 15 '17 at 16:44
  • Could you extend that answer a bit? I've gotten the field with craft.fields.getFieldByHandle() but that doens't seem to work either. – Jan Henckens Dec 20 '17 at 8:10
  • That's strange, you should actually not be able to get the field by handle since matrix fields are unable to be fetched by handle directly That's why I said you should use the id instead. You could use something like {% set partOfChannelA = craft.entries.section('channelA').relatedTo({ targetElement: entryChannelB, field: 'insertYourFieldIdHere' }) %} or leave the field blank if your structure allows it – Robin Schambach Dec 20 '17 at 8:24
  • The problem is probably that I'm trying to do this in a Neo field, not in a Matrix field... – Jan Henckens Dec 20 '17 at 8:43
  • 1
    Ahh now I see.. sorry for my last comment. I did not understand you are going to revert search. This is not possible like stated here github.com/benjamminf/craft-neo/issues/72 I did something similar few weeks ago in this thread craftcms.stackexchange.com/questions/23117/… it works the same way. You would have to select the entry ids by yourself with php with the relation table – Robin Schambach Dec 20 '17 at 9:11
2

I ended up fixing this with a variable and querying it myself.

In this case I needed the related entry if it was linked and a false if it wasn't.

$neoElement = craft()->db->createCommand()
    ->select('craft_relations.sourceId')
    ->from('relations')
    ->where('craft_relations.fieldId = 110')
    ->andWhere('craft_relations.targetId = :entryId', ['entryId' => $entry->id])
    ->queryColumn();

if(!empty($neoElement)) {
    $parents = craft()->db->createCommand()
        ->select('craft_neoblocks.ownerId')
        ->from('neoblocks')
        ->where('craft_neoblocks.id = :neoElementId', ['neoElementId' => $neoElement[0]])
        ->queryColumn();

    if(!empty($parents)) {
        $entry = craft()->entries->getEntryById($parents[0]);
        return $parent;
    }
}

return false;

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.