Пример #1
0
 /**
  * Get a modElement instance taking advantage of the modX::$sourceCache.
  *
  * @param string $class The modElement derivative class to load.
  * @param string $name An element name or raw tagName to identify the modElement instance.
  * @return modElement|null An instance of the specified modElement derivative class.
  */
 public function getElement($class, $name)
 {
     $realname = $this->realname($name);
     if (array_key_exists($class, $this->modx->sourceCache) && array_key_exists($realname, $this->modx->sourceCache[$class])) {
         /** @var modElement $element */
         $element = $this->modx->newObject($class);
         $element->fromArray($this->modx->sourceCache[$class][$realname]['fields'], '', true, true);
         $element->setPolicies($this->modx->sourceCache[$class][$realname]['policies']);
         if (!empty($this->modx->sourceCache[$class][$realname]['source'])) {
             if (!empty($this->modx->sourceCache[$class][$realname]['source']['class_key'])) {
                 $sourceClassKey = $this->modx->sourceCache[$class][$realname]['source']['class_key'];
                 $this->modx->loadClass('sources.modMediaSource');
                 /* @var modMediaSource $source */
                 $source = $this->modx->newObject($sourceClassKey);
                 $source->fromArray($this->modx->sourceCache[$class][$realname]['source'], '', true, true);
                 $element->addOne($source, 'Source');
             }
         }
     } else {
         /** @var modElement $element */
         $element = $this->modx->getObjectGraph($class, array('Source' => array()), array('name' => $realname), true);
         if ($element && array_key_exists($class, $this->modx->sourceCache)) {
             $this->modx->sourceCache[$class][$realname] = array('fields' => $element->toArray(), 'policies' => $element->getPolicies(), 'source' => $element->Source ? $element->Source->toArray() : array());
         }
     }
     if ($element instanceof modElement) {
         $element->set('name', $name);
     }
     return $element;
 }
Пример #2
0
 /**
  * Get a modElement instance taking advantage of the modX::$sourceCache.
  *
  * @param string $class The modElement derivative class to load.
  * @param string $name An element name or raw tagName to identify the modElement instance.
  * @return modElement|null An instance of the specified modElement derivative class.
  */
 public function getElement($class, $name)
 {
     $realname = $this->realname($name);
     if (array_key_exists($class, $this->modx->sourceCache) && array_key_exists($realname, $this->modx->sourceCache[$class])) {
         /** @var modElement $element */
         $element = $this->modx->newObject($class);
         $element->fromArray($this->modx->sourceCache[$class][$realname]['fields'], '', true, true);
         $element->setPolicies($this->modx->sourceCache[$class][$realname]['policies']);
         if (!empty($this->modx->sourceCache[$class][$realname]['source'])) {
             if (!empty($this->modx->sourceCache[$class][$realname]['source']['class_key'])) {
                 $sourceClassKey = $this->modx->sourceCache[$class][$realname]['source']['class_key'];
                 $this->modx->loadClass('sources.modMediaSource');
                 /* @var modMediaSource $source */
                 $source = $this->modx->newObject($sourceClassKey);
                 $source->fromArray($this->modx->sourceCache[$class][$realname]['source'], '', true, true);
                 $element->addOne($source, 'Source');
             }
         }
     } else {
         /** @var modElement $element */
         $element = $this->modx->getObjectGraph($class, array('Source' => array()), array('name' => $realname), true);
         if ($element && array_key_exists($class, $this->modx->sourceCache)) {
             $this->modx->sourceCache[$class][$realname] = array('fields' => $element->toArray(), 'policies' => $element->getPolicies(), 'source' => $element->Source ? $element->Source->toArray() : array());
         } elseif (!$element) {
             $evtOutput = $this->modx->invokeEvent('OnElementNotFound', array('class' => $class, 'name' => $realname));
             $element = false;
             if ($evtOutput != false) {
                 foreach ((array) $evtOutput as $elm) {
                     if (!empty($elm) && is_string($elm)) {
                         $element = $this->modx->newObject($class, array('name' => $realname, 'snippet' => $elm));
                     } elseif ($elm instanceof modElement) {
                         $element = $elm;
                     }
                     if ($element) {
                         break;
                     }
                 }
             }
         }
     }
     if ($element instanceof modElement) {
         $element->set('name', $name);
     }
     return $element;
 }