2

I have created a form using UI component, This form has a select dropdown which displays option related to current entity id if form is in edit mode and all options if a new entity is being added by form.

UI Form

<fieldset name="EntityFields">
    <argument name="data" xsi:type="array">
        <item name="config" xsi:type="array">
            <item name="label" xsi:type="string" translate="true"></item>
        </item>
    </argument>

    <!-- This field represents form id and is hidden -->
    <field name="question_id">
        <argument name="data" xsi:type="array">
            <item name="config" xsi:type="array">                    
                <item name="dataType" xsi:type="string">text</item>
                <item name="formElement" xsi:type="string">input</item>
                <item name="source" xsi:type="string">questionmanager_entity</item>
                <item name="visible" xsi:type="boolean">false</item>
            </item>
        </argument>
    </field>
    <field name="question_type">
        <argument name="data" xsi:type="array">
            <item name="options" xsi:type="object">Ricky\QuestionManager\Model\UiForm\TypeList\Options</item>
            <item name="config" xsi:type="array">
                <item name="component" xsi:type="string">Ricky_QuestionManager\js\form\element\options</item>
                <item name="dataScope" xsi:type="string">question_type</item>
                <item name="dataType" xsi:type="string">text</item>
                <item name="default" xsi:type="string">0</item>
                <item name="formElement" xsi:type="string">select</item>
                <item name="label" xsi:type="string" translate="true">Question Type</item>
                <item name="notice" xsi:type="string" translate="true">Question will use Field type on store front according to selected Question type.</item>
                <item name="required" xsi:type="boolean">true</item>
                <item name="sortOrder" xsi:type="number">30</item>
                <item name="validation" xsi:type="array">
                    <item name="required-entry" xsi:type="boolean">true</item>
                </item>
            </item>
        </argument>
    </field>
</fieldset>

I want to pass current question id to following class (* if form is in edit mode)

<item name="options" xsi:type="object">Ricky\QuestionManager\Model\UiForm\TypeList\Options</item>

*Edit This is my option class

class Options implements OptionSourceInterface
{

/**
 * Product collection
 *
 * @var \Ricky\QuestionManager\Model\ResourceModel\Question\Collection
 */
protected $collection;

/**
 * Construct
 *
 * @param CollectionFactory $collectionFactory
 */
public function __construct(
    CollectionFactory $collectionFactory
) {
    $this->collection = $collectionFactory->create();
}

/**
 * Get options
 *
 * @return array
 */
public function toOptionArray()
{
    foreach ($this->collection as $type) {
        $optionList[] = [
            'label' => $type->getTitle(),
            'value' => $type->getQuestionId(),
            'is_active' => $type->getIsDeleted()?false:true,
        ];
    }
    //echo "<pre>";print_r($optionList);exit;
    return $optionList;
}
}

Thankyou for you suggestion ans answer. :)

  • You can directly get the question id in your options class. – Dharmendra Jadav Mar 22 at 13:49
  • You need to just check the which action has called in your options class. – Dharmendra Jadav Mar 22 at 13:49
  • @DharmendraJadav, I have updated my question with option class. can you please explain me how can i get question id there? – Kapil Dev Singh Mar 25 at 4:46
1

Please check below options class which you have need to change.

class Options implements OptionSourceInterface 
{

/**
 * Http Request class
 *
 * @var \Magento\Framework\App\Request\Http
 */
protected $request;

/**
 * Product collection
 *
 * @var \Ricky\QuestionManager\Model\ResourceModel\Question\Collection
 */
protected $collection;

/**
 * Construct
 *
 * @param CollectionFactory $collectionFactory
 */
public function __construct(
    CollectionFactory $collectionFactory,
    \Magento\Framework\App\Request\Http $request
) {
    $this->collection = $collectionFactory->create();
    $this->request = $request;
}

/**
 * Get options
 *
 * @return array
 */
public function toOptionArray()
{
    $questionId = $this->request->getParam('question_id', false);

    if($questionId) {
        //Your Code which you want
    } else {
        foreach ($this->collection as $type) {
            $optionList[] = [
                'label' => $type->getTitle(),
                'value' => $type->getQuestionId(),
                'is_active' => $type->getIsDeleted()?false:true,
            ];
        }
    }


    //echo "<pre>";print_r($optionList);exit;
    return $optionList;
}
}

Change as per id which you have passed in url and update code as per your need.

Thanks

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.