Storyteller Year Filter Issue

I am interested to know how many people are using Storyteller here?

We have an issues where when create a Campus/Unit Newsroom, there is no automatic way to populate the year selection box on the filter page? We basically have to go in and add a year every time a new year rolls on in the backend script?

https://news.iu.edu/kokomo/news/
https://news.iu.edu/northwest/news/

Also wondering if any one has a custom module solution for this?

Thanks,

Akbar

Hi Akbar,

(I think you’re the only folks using Storyteller here, though any CMS school probably has some overlapping experiences given the closeness in functionality.)

You can use the onBeforeOutput handler to pre-populate custom XPHP variables for things like this, where you want to replace something hard-coded with a little something you could generate in PHP. I’ve added some example custom code to the docs that could be adapted to help you automate those lists of years. Hope this helps!

Karl

Is there a way to pass a variable to the start date depending on what a Storyteller group chooses a year to be from the date attached to stories? Else we will be stuck with creating a custom module for each unit that chooses to use Storyteller?

Thanks.

Hi Akbar,

Sure thing, you could use a custom group field to save a per-group value, and then hook into that using custom code to have different values per-group. Feel free to give that a stab on dev, or you can reach out using your personalized email support address if you’d like hands-on help from our team with that project. Thanks!

Karl

Hi Karl,

We do have a working Custom Module.

<?php
$_LW->REGISTERED_APPS['years_dropdown']=[
     'title'=>'Years Dropdown',
      'handlers' => ['onBeforeOutput']
];

class LiveWhaleApplicationYearsDropdown{

public function onBeforeOutput($buffer) {
	
global $_LW;

if (strpos($buffer, '<xphp var="years_dropdown"')!==false) { // // Populate <xphp var="years_dropdown"/> if it's used
        $GLOBALS['years_dropdown']='<select id="years_dropdown" name="years_dropdown">';
		$GLOBALS['years_dropdown']='';
        for ($i = date("Y"); $i >= 2010; $i--) { // include years from 2010 to the present
                $GLOBALS['years_dropdown'].='<option value="'.$i.'">'.$i.'</option>';
        }
        //$GLOBALS['years_dropdown'].='</select>';
}

return $buffer;

}

}
?>

If we set up a ending year in custom_fields.config.php for every group, just need to how I access the ending year for the group? In this case what do I replace 2010 with?
If you have an example of that, please let us know and we will give it a shot ourselves.
Thanks.

Hm, I don’t have 1-to-1 example code on hand, but if you wanted to try setting up a group custom variable, they get exposed to the page as XPHP variables:

  • Custom fields on the group editor get prefixed with custom_group_ . So, if you have a group setting for contact_info , you’d display it on the front end with <xphp var="custom_group_contact_info" /> . (Source)

XPHP variables match up with $GLOBALS in custom PHP code, so that could mean is when building your $GLOBALS['years_dropdown'] code, you might check for a value of $GLOBALS['custom_group_dropdown_year'] (or whatever you name your custom group variable) and use that in lieu of 2010. Hope this helps!

Will this work for setting variables at the backend and then accessing it in a custom module for a calculation? All the examples just talk about how to display it at the frontend.

I think our issue is that the variable we are setting in custom_fields.config.php is not coming through at the backend and we get 500 error.
Also, is there a way to set the value of a simple test field to something, like 2010? The example just mentions use default option for text fields but says it is a placeholder which is different from value of a field.

Hi Akbar,

Custom variables can be accessed on the backend, yes. You can use $_LW->getCustomFields to grab all custom field values for a specific item in a module setting, or you can leverage the page XPHP variables in some cases using code like this in onOutput or onBeforeOutput:

		if (empty($GLOBALS['group_id'])) {
			$_LW->applyPageAndGroupVars('<xphp var="group_id"/>');
		};

(Replacing group_id with the on-page XPHP variable you need, and then using $GLOBALS['your_specific_variable'] as needed.)

Our SLA prevents me from logging into your site to troubleshoot 500 errors from this Forum, but a few things that might help:

  • You can check /livewhale/?lw_debug=2 when you get a 500 error to see a more detailed report of what the error is logging.
  • To use a custom field number in mathematical code (like the for loop) you likely need to re-cast it as an integer using (int)
  • Regarding a default value: you can use PHP logic to set a default in your module, for example $year = (!empty($GLOBALS['my_variable']) ? (int)$GLOBALS['my_variable'] : 2010);. The same thing can be done on front-end displays using XPHP if/else if needed.

Our Solution:

<?php
$_LW->REGISTERED_APPS['years_dropdown']=[
     'title'=>'Years Dropdown',
      'handlers' => ['onBeforeOutput']
];

class LiveWhaleApplicationYearsDropdown{

public function onBeforeOutput($buffer) {
	
global $_LW;


if (strpos($buffer, '<xphp var="years_dropdown"')!==false) { 
        if(($_LW->applyPageAndGroupVars('<xphp var="group_id"/>')) == 16){ //Kokomo
			$GLOBALS['years_dropdown']='<select id="years_dropdown" name="years_dropdown">';
			$GLOBALS['years_dropdown']='';
			for ($i = date("Y"); $i >= 2019; $i--) { // include years from 2019 to the present
					$GLOBALS['years_dropdown'].='<option value="'.$i.'">'.$i.'</option>';
			}			
		}
        if(($_LW->applyPageAndGroupVars('<xphp var="group_id"/>')) == 20){ //Northwest
			$GLOBALS['years_dropdown']='<select id="years_dropdown" name="years_dropdown">';
			$GLOBALS['years_dropdown']='';
			for ($i = date("Y"); $i >= 2020; $i--) { // include years from 2020 to the present
					$GLOBALS['years_dropdown'].='<option value="'.$i.'">'.$i.'</option>';
			}			
		}
		if(($_LW->applyPageAndGroupVars('<xphp var="group_id"/>')) == 15){ //South Bend
			$GLOBALS['years_dropdown']='<select id="years_dropdown" name="years_dropdown">';
			$GLOBALS['years_dropdown']='';
			for ($i = date("Y"); $i >= 2017; $i--) { // include years from 2017 to the present
					$GLOBALS['years_dropdown'].='<option value="'.$i.'">'.$i.'</option>';
			}			
		}
		if(($_LW->applyPageAndGroupVars('<xphp var="group_id"/>')) == 25){ //Southeast
			$GLOBALS['years_dropdown']='<select id="years_dropdown" name="years_dropdown">';
			$GLOBALS['years_dropdown']='';
			for ($i = date("Y"); $i >= 2020; $i--) { // include years from 202020 to the present
					$GLOBALS['years_dropdown'].='<option value="'.$i.'">'.$i.'</option>';
			}			
		}	
}

return $buffer;

}

}
?>


This is what we came up with. Now, instead of going into every group every year, we will just do it once in one module file when we setup the storyteller group.

Thanks for all your tips and help.