amici-infotech / craft-super-dynamic-fields

Other
3 stars 0 forks source link

Add selected value via PHP #8

Closed ishetnogferre closed 1 year ago

ishetnogferre commented 1 year ago

We're using the dynamic checkboxes on an entry, but we push some data via a plugin and were wondering what we would need to push to the dynamic checkboxes field for it to be selected?

An array with the selected values? An array with all the options and a selected key set on true?

Thanks in advance!

amici-infotech commented 1 year ago

hey, An array with the selected values would be fine.

ishetnogferre commented 1 year ago

Hmm I tried that with following code (we're inserting a new block in a Supertable field which has dynamic checkboxes):

$superTableData["new$index"] = [
      'type' => $blockType->id,
      'enabled' => true,
      'fields' => [
          'offerService' => $item->offerService1->ids(),
          'offerMethod' => $item->offerMethod1->ids(),
          'offerAudience' => $item->offerAudience1->ids(),
          'offerFinances' => $item->offerFinances1->ids(),
      ]
  ];

Which results into the following and the first one is a Category field which gets selected, but the checkboxes not.

array:2 [
  "new1" => array:3 [
    "type" => 1
    "enabled" => true
    "fields" => array:4 [
      "offerService" => array:1 [
        0 => 218
      ]
      "offerMethod" => array:2 [
        0 => 476
        1 => 475
      ]
      "offerAudience" => array:1 [
          0 => 460
      ]
      "offerFinances" => []
    ]
  ]
  "new2" => array:3 [
    "type" => 1
    "enabled" => true
    "fields" => array:4 [
      "offerService" => array:1 [
        0 => 235
      ]
      "offerMethod" => []
      "offerAudience" => []
      "offerFinances" => []
    ]
  ]
Screenshot 2023-09-20 at 20 14 44
amici-infotech commented 1 year ago

This is my working test code. Hope that helps.

$entry = \craft\elements\Entry::find()->id(350)->one();
$entry->setFieldValue('superTable', [
    'sortOrder' => [
        'new1',
        'new2'
    ],
    'blocks' => [
        'new1' => [
            'type' => "1",
            'fields' => [
                'entriesRelationship' => [
                    '319',
                    '314'
                ],
                'normalCheckboxes' => [
                    'option2',
                    'option4',
                ],
                'sdfDropdown' => 'option2',
                'sdfCheckboxesStaticOptions' => [
                    'option1',
                    'option3',
                ],
                'sdfCheckboxesDynamicOptions' => [
                    '362',
                    '364',
                ],
            ]
        ],
        'new2' => [
            'type' => "1",
            'fields' => [
                'entriesRelationship' => [
                    '319',
                    '314'
                ],
                'normalCheckboxes' => [
                    'option2',
                    'option4',
                ],
                'sdfDropdown' => 'option2',
                'sdfCheckboxesStaticOptions' => [
                    'option1',
                    'option3',
                ],
                'sdfCheckboxesDynamicOptions' => [
                    '362',
                    '364',
                ],
            ]
        ]
    ]
]);

$saved = \Craft::$app->getElements()->saveElement($entry);
echo $saved;
exit;

image

ishetnogferre commented 1 year ago

Aha thanks, that helped! Needed to convert the int values from the ids() call to strings 😅