1. NOTICE: If you are using Fabrik and update to Joomla 3.10, you will need to update to Fabrik 3.10. And, if you are using Fabrik, do not upgrade to Joomla 4, we do not have a supported version ready for release. More information on a release date coming soon. Also, please note that Fabrik 3.10 will not install on any Joomla sites less than 3.8.
    Dismiss Notice

Initially setting an element filter on first access

Discussion in 'Community' started by achartier, Apr 12, 2018.

  1. achartier

    achartier Active Member

    Level: Community
    I would be interested to know if there is a better way to do this.

    I have a status element for a list of clients, values Active, Inactive & Special. I have created an element filter on this element. When the list initially loads and displays, the element filter is set to All. What I want is that the first time the list is loaded the status element filter is set to Active. If the user changes this to All I want all records to display.

    I tried a pre-filter but this filters the whole table and only the filtered rows will ever display. I tried adding the filter to the menu but same results.

    What I ended up doing is adding a php_event plugin and inserted the following code into the onPreLoadData function. It works but I wonder if there might be an easier way. Any comments/ideas/suggestions most welcome.

    Code (Text):
    <?php
    /**
    * Script to set the intial state of the clients status filter to active.
        Once we go through this the first time in a session, we set a session
        variable to indicate this has been done once, sugbsequent visits to the
        page will remember the last status filter and we will allow a selection of All.
    */
    $package = $this->app->getUserState('com_fabrik.package', 'fabrik');
    $context = 'com_' . $package . '.list' . $model->getRenderContext() . '.initialDisplay';
    $session = JFactory::getSession();
    if (!is_null($session->get($context))) return;

    $filters = $model->filters;
    if (count($filters) == 0) {
    $model->filters = array(
            'access' => array(0 =>'1'),
            'condition' => array(0 =>'='),
            'elementid' => array(0 => '274'),
            'filter' => array(0 =>'1'),
            'eval' => array(0 => '0'),
            'full_words_only' => array(0 =>'0'),
            'grouped_to_previous' => array(0 =>'0'),
            'hidden' => array(0 =>'0'),
            'join' => array(0 =>'AND'),
            'key' => array(0 => "`tqkjo_user_credits_by_type`.`status`"),
            'label' => array(0 =>'Status'),
            'match' => array(0 =>'1'),
            'no-filter-setup' => array(0 =>'0'),
            'orig_condition' => array(0 =>'0'),
            'origvalue' => array(0 =>'1'),
            'raw' => array(0 =>false),
            'required' => array(0 =>'0'),
            'search_type' => array(0 =>'normal'),
            'sqlCond' => array(0 => "(`tqkjo_user_credits_by_type`.`status` = '1' OR `tqkjo_user_credits_by_type`.`status` LIKE '[\"1\"%' OR `tqkjo_user_credits_by_type`.`status` LIKE '%\"1\"%' OR `tqkjo_user_credits_by_type`.`status` LIKE '%\"1\"]')"),
            'value' => array(0 =>'1'),
            );
    $model->getFilterArray($model->filters);
    $session->set($context, true);
    }
     
  2. Sophist

    Sophist Well-Known Member

    Level: Community
    I have not done this myself, but according to the editing screen you can make a pre-filter either permanent or replaceable. If you set a replaceable prefilter of "Active" then the user should be able to change it to "All". Did you try this? And what happened when you did?
     
  3. achartier

    achartier Active Member

    Level: Community
    Thanks Sophist but I do not see the permanent/replaceable. Is this somewhere on the list pre-filter tab or somewhere else?
     
  4. Sophist

    Sophist Well-Known Member

    Level: Community
    Sorry - that will teach me not to go and check the precise details.

    The setting I was referring to was actually "Overridden by menu / modules" which is not the same thing at all.

    So you are right that you cannot do it with a pre-filter. But you can do it by adding filter parameters to the menu entry that loads the list as shown in the Wiki at http://fabrikar.com/forums/index.php?wiki/filtering-lists-tables/#filtering-via-the-url
     
  5. Sophist

    Sophist Well-Known Member

    Level: Community
    P.S. Impressed by the technical knowledge displayed in your solution in the original question. :) You should consider contributing code for fixes and small enhancements.
     
  6. achartier

    achartier Active Member

    Level: Community
    I tried the url override but that also limited the available records to only the records of this filter, so changing the element filter resulted in no records found.

    Thanks for the atta-boy. I actually spent a fair amount of time working on getting FullCalendar operational with Hugh so I have contributed a fair amount.
     
  7. achartier

    achartier Active Member

    Level: Community
    Following up on my original posting I have run into another small issue. The initial filter that I set is not being saved in the session so it isn't remembered. I have looked through the code and haven't been able to determine an easy way to set this filter into the session so that on subsequent visits during the same session the filter is remembered.

    To further clarify what is happening, when the list is first displayed the filter is set by my plugin to show Active clients. If the user browses away from the page and then comes back my session variable is set so I do not set the filter again, and since there are no filters saved in the session the list defaults to All. This is complicated by the fact that if the user selects the All filter this clears the session filters.

    If anyone has an ideas on how to set the filters into the session I am all ears.
     
  8. Sophist

    Sophist Well-Known Member

    Level: Community
    I'm afraid that you are now beyond my knowledge. You may need Hugh's help for this, and given his statement about time for community support being "vanishingly small", you may need to think about a subscription.
     
  9. achartier

    achartier Active Member

    Level: Community
    I think I have it sorted. Once I have it all cleaned up I will post the final code here for others.
     
  10. Sophist

    Sophist Well-Known Member

    Level: Community
    Your use case for having the pre-filter not permanent is probably not unique, though I suspect the majority of use cases do want the pre-filter to be permanent.

    From the brief looks I have taken at the filtering code, I suspect that it would not be that hard to add a switch to make pre-filters temporary for Lists, Menus and Modules. But something like that is quite core and would really need Hugh to do it.
     
  11. troester

    troester Well-Known Member Staff Member

    Level: Community
    Did you try to filter the list via URL?
    There's no option in list menu settings to append parameters so you'll need a menu type SystemLink/URL with something like
    index.php?option=com_fabrik&view=list&listid=1&tqkjo_user_credits_by_type___status=1
     
  12. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    If all you are trying to do is initially set a filter value when the menu item is selected, then Troester's solution is the easiest.

    I could add the same "Extra Query String" option we have for form menu items to the list menu item, if that would help. It's only a few lines of code, mostly copied and pasted from the form controller.

    -- hugh
     
  13. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Hmmm, having said that, I'm not sure if that would work, it might override the actual selected value.

    -- hugh
     
  14. achartier

    achartier Active Member

    Level: Community
    The URL override seems to work, but, only if I set the URL as an SEF URL, if I use the format troester recommended I get an error Call to member function GetData() on Null. I haven't had a chance to see yet where that error is occurring, I will try and have a look in the morning.
     
  15. achartier

    achartier Active Member

    Level: Community
    I need to backtrack a little here. Ignore my previous post.

    The problem seems to have something to do with SEF URLs being enabled. If I turn off SEF and use the following URL in a system URL menu link it works: index.php?option=com_fabrik&view=list&listid=16&client_data___status=1

    But as soon as I turn on SEF it breaks and I get an error thrown by my template (Helix3 Shaper) "Call to a member function getData() on null". I haven't been able to track down yet which file is throwing that error. I will keep looking, but there seems to be some issue with adding params and SEF.
     
  16. troester

    troester Well-Known Member Staff Member

    Level: Community
    It's working on my site with and without SEF.
    But
    With SEF enabled it's displaying the filtered list correctly but it's setting the active menu item to Home. Which menu type is your "Home" (index.php)?

    It's the same if I use a URL menu link to an article (index.php?option=com_content&view=article&id=7), so this seems to be a Joomla issue, not Fabrik.
    I have a multilanguage site but is doesn't matter if I add '&lang=de' to the URL or not.
     
  17. achartier

    achartier Active Member

    Level: Community
    Further info, for some reason the J! router is passing input to fabrik.php without including the query data, so that when fabrik.php tries to build the controller it ends up defaulting to a form view which then gets completely lost because there is no form defined in the input.

    This could very well be a J! issue but I have no idea where to go next with it.
     
  18. troester

    troester Well-Known Member Staff Member

    Level: Community
    It's pretty strange how the menu item is switching in case of SEF enabled.

    I tried an other menu item as "Home", then the URL to content was not longer working (showing some categories instead of the article).

    With a full "external" URL it's showing the article, but still activating "Home" instead of staying on the choosen menu item (which I think makes sense in this case because it doesn't append the Itemid if it's an external URL).

    With a "local" URL (index.php?com_whatever) it IS appending the Itemid but seems to ignore it in case of SEF.

    Try a full URL + add the Itemid (http://your-site/index.php?option=com_fabrik&view=list&listid=16&client_data___status=1&Itemid=X)
    This seems to do in my case, with SEF enabled it's staying on the menu item and displaying the correct things.
     
  19. achartier

    achartier Active Member

    Level: Community
    Well yes, that would work because it isn't going through the SEF router, but what I am left with is a non-SEF url in the address bar, and I have to remember when I restore to a backup testing site to change any of these external URLs to the address of the test site.

    I don't see what the Itemid has to do with anything. Yes J! is appending it to the SEF URL but the J! router is finding fabrik (the home page menu item is just an article) but J! is not setting the input data correctly.

    Something is definitely wrong in SEF land though.
     
  20. achartier

    achartier Active Member

    Level: Community
    Sorry, the above may have come off as a little flippant, wasn't meant to be.
     

Share This Page