We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 19328
    • 433 Posts
    Hello,

    I'm trying to create a custom filter for the Russian addon mFilter2. I'm getting somewhere I think, but I'm having some trouble figuring out exactly how to do this. I need to filter on the tvvalues of the parents of the resources.

    I already have this:

    <?php
    class showParentTv extends mse2FiltersHandler {
    
        /**
    	 * Prepares values for filter
    	 * Returns array with human-readable parents of resources
    	 *
    	 * @param array $values
    	 * @param string $name Filter name
    	 * @param integer $depth
    	 * @param string $separator
    	 *
    	 * @return array Prepared values
    	 */
    	public function getShowparenttvValues(array $values, $name = '', $depth = 1, $separator = ' / ') {
    		
    
    	$this->modx->log(modX::LOG_LEVEL_ERROR,'getShowparenttvValues start');
    
    	// get all the parent resources we need
    	$values = $this->modx->getChildIds(25,1,array('context' => 'web'));
    
    	//for mfilter2 (copied from other filter)
    	$keys = array_keys($values);
    	if (empty($keys) || (count($keys) < 2 && empty($this->config['showEmptyFilters']))) {
    			return array();
    	}
    
    	$tvId = 41;
    
    	// loop the ids
    	foreach ($values as $value => $ids) {
    		if ($value !== '') {
    				
    				// get tvvalue for each resource
    				$tv = $this->modx->getObject('modTemplateVar', $tvId);
    				$val = $tv->getValue($ids);
    
    				//$this->modx->log(modX::LOG_LEVEL_ERROR,'getShowparenttvValues tvvalue: '.$val);
    
    				// explode val. Result is array with separate numbers (ids of resources)
    				$separateTvValues = explode("||", $val);
    
    				//for each value get pagetitle
    				foreach ($separateTvValues as $SchoolResource) {
    					$page = $this->modx->getObject('modResource', $SchoolResource);
    					$pagetitle = $page->get('pagetitle');
    				}
    
    				// connect it all...
    				// pagetitle = name of filter option (schoolname)
    				// id's of all child resources who's parents have the schoolname in the tv 41
    				
    
    				// finally, this needs to come out for mFilter2:
    				$title = trim(
    					isset($names[$value])
    						? $names[$value]
    						: $value
    				);
    				$results[$title] = array(
    					'title' => $title, // I think should contain name of filter option
    					'value' => $value, // same
    					'resources' => $ids, // ids of resources who's parents have this specific tv value
    				);
    			}
    		}
    		ksort($results);
    
    		return $results;
    	}
    
    }


    My trouble is in getting and connecting all the results an returning it in a way that is useful for mFilter2.

    Does anyone know how I can fill in the missing part?

    Thanks for any help!