1

I'm seeting up all of my project's fields using migrations. This works really well, but I haven't been able to figure out how to limit the sources (sections) for an entry selection field.

I've based my work on this migrations example, which contains the following code:

new \craft\fields\Entries([
    "name" => "Questions",
    "handle" => "questions",
    "instructions" => "Choose one or more questions",
    "sources" => ["section:" . $questionsSection->id],
    "limit" => 10,
    "required" => false,
    "selectionLabel" => "Select Questions",
]),

However, I don't get how I can add multiple sections as allowed sources… can anybody clarify how to do this?

  • Could you add your update as an official answer instead of an update to the original question? – Brad Bell Jul 29 at 18:19
  • @BradBell, done. Thanks for pointing that out. – JSICS Jul 30 at 11:04
1

After getting some feedback from Craft support it turns out one has to use uid instead of id for this to work:

"sources" => ["section:" . $questionsSection->uid],

Since I had a lot of places in my migration where I needed to specify sources, I included a couple small functions:

/* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */

protected function _section_id($sectionHandle)
{
    return Craft::$app->sections->getSectionByHandle($sectionHandle)->uid;

} // _section_id()

/* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */

protected function _source($handle, $kind = 'section')
{
    $source = $kind . ':' . $this->_section_id($handle);
    return $source;

} // _source()

/* --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- */

You can then limit the entries field sources like so:

"source" => null,
"sources" => [
    $this->_source("sectionA"),
    $this->_source("sectionB"),
],

(Note that I'm additionally setting "source" => null as per a plugin-created migration I analyzed, but not sure if it's required.)

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.