RainLab\Pages\Classes\Snippet::listPageComponents PHP Method

listPageComponents() public static method

Returns a list of component definitions declared on the page.
public static listPageComponents ( string $pageName, Cms\Classes\Theme $theme, $markup ) : array
$pageName string Specifies the static page file name (the name of the corresponding content block file).
$theme Cms\Classes\Theme Specifies a parent theme.
return array Returns an array of component definitions
    public static function listPageComponents($pageName, $theme, $markup)
    {
        $map = self::extractSnippetsFromMarkupCached($theme, $pageName, $markup);
        $result = [];
        foreach ($map as $snippetDeclaration => $snippetInfo) {
            if (!isset($snippetInfo['component'])) {
                continue;
            }
            $result[] = ['class' => $snippetInfo['component'], 'alias' => $snippetInfo['code'], 'properties' => $snippetInfo['properties']];
        }
        return $result;
    }

Usage Example

Example #1
0
 /**
  * Initializes CMS components associated with the page.
  */
 public function initCmsComponents($cmsController)
 {
     $snippetComponents = Snippet::listPageComponents($this->getFileName(), $this->theme, $this->markup . $this->code);
     $componentManager = ComponentManager::instance();
     foreach ($snippetComponents as $componentInfo) {
         // Register components for snippet-based components
         // if they're not defined yet. This is required because
         // not all snippet components are registered as components,
         // but it's safe to register them in render-time.
         if (!$componentManager->hasComponent($componentInfo['class'])) {
             $componentManager->registerComponent($componentInfo['class'], $componentInfo['alias']);
         }
         $cmsController->addComponent($componentInfo['class'], $componentInfo['alias'], $componentInfo['properties']);
     }
 }