Help with my twig and looping over collection
Created 7 years ago by edster

Few things to note, I know how to do this in PHP in a module. I did not use a module but the streams addon, so we are stuck with twig here.

Looking for help in how I'm SUPPOSED TO do this. Please note do not say use a module or something like that, that isn't the point here.

I have 2 streams Office and Staff

There is a many to many relationship between them where Staff has a relationship field offices. This is a many to many.

I originally tried and wanted to just pull staff members who are in the vancouver office, but couldn't figure out how. So I then thought I would try to get all staff members and just show the ones that are in that office.

I have tried a bunch of things, the two below being the more elegant, keep in mind none of these work.

{% for member in staff %}
   {% if member.offices.search('Vanccouver') %}
      DO SHIT
   {% endif %}
{% endfor %}
{% for member in staff %}
   {% if myVal in member.offices %}
      DO SHIT
   {% endif %}
{% endfor %}

I COULD do it like below, but my god, is it retarded and ugly and there HAS to be a better way

{% for member in staff %}
   {% for office in member.offices %}
      {% if (office.value == myVal) %}
           DO SHIT
      {% endif %}
    {% endfor %}
{% endfor %}

Any help would be great

ryanthompson  —  7 years ago Best Answer

Ok I see what was happening - I pushed a patch to undecorate before running that findBy test. Should be good!

piterden  —  7 years ago

   {% if myVal in member.offices %}
      DO SHIT
   {% endif %}
{% endfor %}```
Are you absolutely sure about `in` inside `if`
edster  —  7 years ago

Yea it is a twig function

my guess is it isn't a straight array but a collection but I don't know how to get it into a usable array

keevitaja  —  7 years ago

@edster var in member.offices.toArray()

in case that was a question

edster  —  7 years ago

Hum closer....

{% for member in staff %}
   {% if member.offices.findBy(,'name', 'Vancouver') %}
      DO SHIT
   {% endif %}
{% endfor %}

Would work IF https://github.com/anomalylabs/streams-platform/blob/master/src/Model/EloquentCollection.php#L82 was

$entry->{$key}->value === $value

From there I was like hummmmm maybe if I pass in the object it might work

member.offices.findBy('name', page.office)

This doesn't work eaither.

BUT I get the below which looks basically the same to me lol

Below is result of $value passing in page.office

FieldTypePresenter {#3086 ▼
  #object: TextFieldType {#3084 ▼
    #inputView: "anomaly.field_type.text::input"
    #config: array:8 [▶]
    #disabled: false
    #readonly: false
    #rules: []
    #validators: []
    #messages: []
    #entry: CorporateInfoOfficesEntryModel {#3119 ▶}
    #field: "name"
    #value: "Vancouver"
    #label: "Name"
    #warning: null
    #locale: null
    #attributes: []
    #instructions: null
    #placeholder: null
    #required: "1"
    #hidden: false
    #prefix: null
    #class: null
    #columnType: "string"
    #columnLength: null
    #filterView: "streams::form/partials/filter"
    #wrapperView: "streams::form/partials/wrapper"
    #presenter: "Anomaly\Streams\Platform\Addon\FieldType\FieldTypePresenter"
    #modifier: FieldTypeModifier {#1205 ▶}
    #accessor: FieldTypeAccessor {#3076 ▶}
    #schema: null
    #parser: null
    #query: null
    #path: "C:\Sites\commercial_martello_group\core/anomaly/text-field_type"
    #type: "anomaly.field_type.text"
    #slug: "text"
    #vendor: "anomaly"
    #namespace: "anomaly.field_type.text"
    #callbacks: []
  }
  #protected: array:3 [▶]
}

Below is a dump of the entries key where key is the result of passing in the entry member.offices

FieldTypePresenter {#3253 ▼
  #object: TextFieldType {#3084 ▼
    #inputView: "anomaly.field_type.text::input"
    #config: array:10 [▶]
    #disabled: false
    #readonly: false
    #rules: []
    #validators: []
    #messages: []
    #entry: CorporateInfoOfficesEntryModel {#3081 ▶}
    #field: "name"
    #value: "Vancouver"
    #label: "Name"
    #warning: null
    #locale: null
    #attributes: []
    #instructions: null
    #placeholder: null
    #required: "1"
    #hidden: false
    #prefix: null
    #class: null
    #columnType: "string"
    #columnLength: null
    #filterView: "streams::form/partials/filter"
    #wrapperView: "streams::form/partials/wrapper"
    #presenter: "Anomaly\Streams\Platform\Addon\FieldType\FieldTypePresenter"
    #modifier: FieldTypeModifier {#1205 ▶}
    #accessor: FieldTypeAccessor {#3076 ▶}
    #schema: null
    #parser: null
    #query: null
    #path: "C:\Sites\commercial_martello_group\core/anomaly/text-field_type"
    #type: "anomaly.field_type.text"
    #slug: "text"
    #vendor: "anomaly"
    #namespace: "anomaly.field_type.text"
    #callbacks: []
  }
  #protected: array:3 [▶]
}
ryanthompson  —  7 years ago

Are you doing a string search? Like str_pos?

ryanthompson  —  7 years ago Best Answer

Ok I see what was happening - I pushed a patch to undecorate before running that findBy test. Should be good!