Filter Entity Reference View with dynamic arguments from Reference Field












0















I need to filter an "Entity Reference View" autocomplete widget by passing arguments from a "Reference Field" in my content type.



Autocomplete by URL



I researched on this and found that PHP Code type Contextual Filter is the most suggested way to achieving this but as PHP Code has now been removed from Drupal 8 Core, what are the alternatives to this common use case? (an earlier suggestion to use PHP Code: Drupal 7: how to filter view content (with entity reference field) based on current page content)



While editing the reference field, it seems that only hard-coded values be mentioned in "View arguments" field and there is no dynamical way of achieving this (e.g. from URL/query string etc).



View Arguments field










share|improve this question























  • What do you mean by "entity reference view" ... please give more detail of your setup

    – GiorgosK
    Aug 18 '18 at 3:45











  • I'm working on Drupal 8. I setup a view of "Entity Reference" type and then mentioned it in my "Entity Reference" field (autocomplete widget) in a Content Type. You can notice it in 2nd picture above "View used to select the entities". My question is about "View arguments" field (in the same picture) where I'm trying to pass dynamic parameters (for example if "Country ISO Code=USA" then autocomplete widget only shows USA office locations and not all offices around the world). Please let me know if I was able to clarify. Thanks for your support

    – Farrukh
    Aug 18 '18 at 9:23
















0















I need to filter an "Entity Reference View" autocomplete widget by passing arguments from a "Reference Field" in my content type.



Autocomplete by URL



I researched on this and found that PHP Code type Contextual Filter is the most suggested way to achieving this but as PHP Code has now been removed from Drupal 8 Core, what are the alternatives to this common use case? (an earlier suggestion to use PHP Code: Drupal 7: how to filter view content (with entity reference field) based on current page content)



While editing the reference field, it seems that only hard-coded values be mentioned in "View arguments" field and there is no dynamical way of achieving this (e.g. from URL/query string etc).



View Arguments field










share|improve this question























  • What do you mean by "entity reference view" ... please give more detail of your setup

    – GiorgosK
    Aug 18 '18 at 3:45











  • I'm working on Drupal 8. I setup a view of "Entity Reference" type and then mentioned it in my "Entity Reference" field (autocomplete widget) in a Content Type. You can notice it in 2nd picture above "View used to select the entities". My question is about "View arguments" field (in the same picture) where I'm trying to pass dynamic parameters (for example if "Country ISO Code=USA" then autocomplete widget only shows USA office locations and not all offices around the world). Please let me know if I was able to clarify. Thanks for your support

    – Farrukh
    Aug 18 '18 at 9:23














0












0








0








I need to filter an "Entity Reference View" autocomplete widget by passing arguments from a "Reference Field" in my content type.



Autocomplete by URL



I researched on this and found that PHP Code type Contextual Filter is the most suggested way to achieving this but as PHP Code has now been removed from Drupal 8 Core, what are the alternatives to this common use case? (an earlier suggestion to use PHP Code: Drupal 7: how to filter view content (with entity reference field) based on current page content)



While editing the reference field, it seems that only hard-coded values be mentioned in "View arguments" field and there is no dynamical way of achieving this (e.g. from URL/query string etc).



View Arguments field










share|improve this question














I need to filter an "Entity Reference View" autocomplete widget by passing arguments from a "Reference Field" in my content type.



Autocomplete by URL



I researched on this and found that PHP Code type Contextual Filter is the most suggested way to achieving this but as PHP Code has now been removed from Drupal 8 Core, what are the alternatives to this common use case? (an earlier suggestion to use PHP Code: Drupal 7: how to filter view content (with entity reference field) based on current page content)



While editing the reference field, it seems that only hard-coded values be mentioned in "View arguments" field and there is no dynamical way of achieving this (e.g. from URL/query string etc).



View Arguments field







drupal drupal-8






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Aug 17 '18 at 15:55









FarrukhFarrukh

97210




97210













  • What do you mean by "entity reference view" ... please give more detail of your setup

    – GiorgosK
    Aug 18 '18 at 3:45











  • I'm working on Drupal 8. I setup a view of "Entity Reference" type and then mentioned it in my "Entity Reference" field (autocomplete widget) in a Content Type. You can notice it in 2nd picture above "View used to select the entities". My question is about "View arguments" field (in the same picture) where I'm trying to pass dynamic parameters (for example if "Country ISO Code=USA" then autocomplete widget only shows USA office locations and not all offices around the world). Please let me know if I was able to clarify. Thanks for your support

    – Farrukh
    Aug 18 '18 at 9:23



















  • What do you mean by "entity reference view" ... please give more detail of your setup

    – GiorgosK
    Aug 18 '18 at 3:45











  • I'm working on Drupal 8. I setup a view of "Entity Reference" type and then mentioned it in my "Entity Reference" field (autocomplete widget) in a Content Type. You can notice it in 2nd picture above "View used to select the entities". My question is about "View arguments" field (in the same picture) where I'm trying to pass dynamic parameters (for example if "Country ISO Code=USA" then autocomplete widget only shows USA office locations and not all offices around the world). Please let me know if I was able to clarify. Thanks for your support

    – Farrukh
    Aug 18 '18 at 9:23

















What do you mean by "entity reference view" ... please give more detail of your setup

– GiorgosK
Aug 18 '18 at 3:45





What do you mean by "entity reference view" ... please give more detail of your setup

– GiorgosK
Aug 18 '18 at 3:45













I'm working on Drupal 8. I setup a view of "Entity Reference" type and then mentioned it in my "Entity Reference" field (autocomplete widget) in a Content Type. You can notice it in 2nd picture above "View used to select the entities". My question is about "View arguments" field (in the same picture) where I'm trying to pass dynamic parameters (for example if "Country ISO Code=USA" then autocomplete widget only shows USA office locations and not all offices around the world). Please let me know if I was able to clarify. Thanks for your support

– Farrukh
Aug 18 '18 at 9:23





I'm working on Drupal 8. I setup a view of "Entity Reference" type and then mentioned it in my "Entity Reference" field (autocomplete widget) in a Content Type. You can notice it in 2nd picture above "View used to select the entities". My question is about "View arguments" field (in the same picture) where I'm trying to pass dynamic parameters (for example if "Country ISO Code=USA" then autocomplete widget only shows USA office locations and not all offices around the world). Please let me know if I was able to clarify. Thanks for your support

– Farrukh
Aug 18 '18 at 9:23












1 Answer
1






active

oldest

votes


















0














It's hacky, but you can use a hook_form_alter. Here's an example passing the current NID as an argument:



/**
* Implements hook_form_alter().
*/
function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id)
{

if ($form_id == 'node_NODETYPE_edit_form') {
$node = $form_state->getFormObject()->getEntity();
$form['field_MY_REF_FIELD']['widget'][0]['target_id']['#selection_settings']['view']['arguments'][0] = $node->id();
}
}





share|improve this answer























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51899076%2ffilter-entity-reference-view-with-dynamic-arguments-from-reference-field%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    It's hacky, but you can use a hook_form_alter. Here's an example passing the current NID as an argument:



    /**
    * Implements hook_form_alter().
    */
    function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id)
    {

    if ($form_id == 'node_NODETYPE_edit_form') {
    $node = $form_state->getFormObject()->getEntity();
    $form['field_MY_REF_FIELD']['widget'][0]['target_id']['#selection_settings']['view']['arguments'][0] = $node->id();
    }
    }





    share|improve this answer




























      0














      It's hacky, but you can use a hook_form_alter. Here's an example passing the current NID as an argument:



      /**
      * Implements hook_form_alter().
      */
      function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id)
      {

      if ($form_id == 'node_NODETYPE_edit_form') {
      $node = $form_state->getFormObject()->getEntity();
      $form['field_MY_REF_FIELD']['widget'][0]['target_id']['#selection_settings']['view']['arguments'][0] = $node->id();
      }
      }





      share|improve this answer


























        0












        0








        0







        It's hacky, but you can use a hook_form_alter. Here's an example passing the current NID as an argument:



        /**
        * Implements hook_form_alter().
        */
        function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id)
        {

        if ($form_id == 'node_NODETYPE_edit_form') {
        $node = $form_state->getFormObject()->getEntity();
        $form['field_MY_REF_FIELD']['widget'][0]['target_id']['#selection_settings']['view']['arguments'][0] = $node->id();
        }
        }





        share|improve this answer













        It's hacky, but you can use a hook_form_alter. Here's an example passing the current NID as an argument:



        /**
        * Implements hook_form_alter().
        */
        function MYMODULE_form_alter(&$form, FormStateInterface $form_state, $form_id)
        {

        if ($form_id == 'node_NODETYPE_edit_form') {
        $node = $form_state->getFormObject()->getEntity();
        $form['field_MY_REF_FIELD']['widget'][0]['target_id']['#selection_settings']['view']['arguments'][0] = $node->id();
        }
        }






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 24 '18 at 19:54









        BoinksterBoinkster

        1




        1
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f51899076%2ffilter-entity-reference-view-with-dynamic-arguments-from-reference-field%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Feedback on college project

            Futebolista

            Albești (Vaslui)