Skip to content

Instantly share code, notes, and snippets.

@eonarik
Last active July 1, 2017 15:30
Show Gist options
  • Save eonarik/fcfd5b7848903529906cd1026b11a7a4 to your computer and use it in GitHub Desktop.
Save eonarik/fcfd5b7848903529906cd1026b11a7a4 to your computer and use it in GitHub Desktop.
modx processor Breadcrumbs
<?php
class modWebBreadcrumbsGetdataProcessor extends modProcessor{
public $items = array();
public $defaultFields = array(
'pagetitle',
'longtitle',
'menutitle',
'uri'
);
public function initialize(){
$this->setDefaultProperties(array(
'showHomeLink' => true, // показывать ссылку на главную
'showHome' => false, // показывать крошки на главной
'showhidden' => false, // показывать скрытые в меню
'homeTitle' => 'Главная', // menutitle главной страницы
'includeTVs' => array(), // включить tv в вывод
));
// вывод определенных полей документа
if($output_fields = $this->getProperty('outputFields')){
$this->defaultFields = array_merge(
$this->defaultFields,
$output_fields
);
}
return parent::initialize();
}
public function process() {
$site_start = $this->modx->getOption('site_start');
$object =& $this->modx->resource;
if(
!$this->getProperty('showHome')
&& $object->id != $site_start
|| $this->getProperty('showHome')
) {
$this->getParents($object);
if($this->getProperty('showHomeLink')){
$object_mainpage = $this->modx->getObject('modResource', $site_start);
$this->items[] = array_merge(
$this->setFields($object_mainpage),
array(
'linktext' => $this->getProperty('homeTitle') ? $this->getProperty('homeTitle') : $object_mainpage->pagetitle,
'is_home' => true
)
);
}
}
return array(
'success' => true,
'message' => '',
'object' => array_reverse($this->items),
);
}
// идем вглубь по дереву
public function getParents($object) {
$parent = $object->parent;
if(
$object->published
&& !$object->deleted
&& $object->checkPolicy('some_permission')
){
if(
!$this->getProperty('showhidden')
&& !$object->hidemenu
){
$this->items[] = $this->setFields($object);
}
}
if($parent != 0){
$this->getParents($this->modx->getObject('modResource', $parent));
}
}
// формируем выходящий массив
public function setFields($object){
$fields = array();
foreach($object->toArray() as $key=>$fld) {
if(in_array($key, $this->defaultFields)) {
$fields[$key] = $fld;
}
// устанавляваем флаг is_current для текущего документа
if($key == 'id'){
if($fld == $this->modx->resource->id) {
$fields['is_current'] = true;
}
}
}
$fields['linktext'] = $fields['menutitle'] ? $fields['menutitle'] : $fields['pagetitle'];
if(
$tvs = $this->getProperty('includeTVs')
AND !empty($tvs)
AND is_array($tvs)
){
foreach($tvs as $tv){
if(is_numeric($tv)){
$tv = $this->modx->getObject('modTemplateVar', $tv)->name;
}
$fields[$tv] = $object->getTVValue($tv);
}
}
return $fields;
}
}
return 'modWebBreadcrumbsGetdataProcessor';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment