Magento 1.9 – How to Get a List of All Blocks with Their Names

blocksmagento-1magento-1.9PHP

I'm trying to find a way to get a list of all magento 1.9 blocks with their names programmatically.

I.e:

$blocks= [
    'core/text_tag_css_admin' => 'Mage_Core_Block_Text_Tag_Css_Admin',
    'core/text_list_item' => 'Mage_Core_Block_Text_List_Item',
    'core/text_list_link' => 'Mage_Core_Block_Text_List_Link',
]

I searched but I haven't found anything on how to approach this problem.

Here's the code I have so far in case it will help someone else.

        public function toOptionArray()
        {
            /**
             * @var $blocks Mage_Core_Model_Config_Element
             */
            $blocks = Mage::getConfig()->getNode('global/blocks')->asArray();

            $classes = [];
            $options = [];
            $realClasses = [];


            foreach ($blocks as $key => $value) {
                $classes[$key] = $value['class'];
            }


            foreach ($classes as $key => $class) {
                $classKeys = explode('_', $class);
                $moduleDir = Mage::getModuleDir('', $classKeys[0] . '_' . $classKeys[1]);
                $moduleDir = $moduleDir . DS . $classKeys[2];
                if (empty($moduleDir))
                    continue;
                $files = [];
                $files = $this->listFIles($moduleDir);
                $relativeClasses = $this->getRelativeClasses($files, $moduleDir);
                foreach ($relativeClasses as $relativeClass) {
                    $realClass = $class . $relativeClass;
                    $realClasses[] = $realClass;
                }
            }

            $result = [];
            sort($realClasses);
        //TODO: Eliminate abstract classes
        foreach ($realClasses as $realClass) {
            try {
                //throws fatal error so can't be caught
                $r = new ReflectionClass($realClass);
            } catch (Exception $e) {

            }
            if (isset($r) && !$r->isAbstract())
                $result[] = array('value' => $realClass, 'label' => $realClass);
        }
        return $result;

        }


        protected function getRelativeClasses($files, $moduleDir)
        {
            if (empty($files) || !file_exists($moduleDir))
                return [];
            $classes = [];
            foreach ($files as $file) {
                $filePath = $file->getPathname();
                $relativeClassPath = str_replace($moduleDir, '', $filePath);
                $relativeClassPath = str_replace('.php', '', $relativeClassPath);
                $relativeClassPath = str_replace(DS, '_', $relativeClassPath);
                $classes[] = $relativeClassPath;
            }

            return $classes;
        }


        protected function listFIles($dir)
        {
            if (!file_exists($dir))
                return [];
            $files = [];
            $di = new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS);
            $it = new RecursiveIteratorIterator($di);

            foreach ($it as $file) {
                if (pathinfo($file, PATHINFO_EXTENSION) == "php") {
                    $files[] = $file;
                }
            }
            return $files;
        }

Best Answer

You can find them in two steps:

  1. find all module aliases:

    $aliases = Mage::getConfig()->getNode('global/blocks');
    

    using n98-magerun dev:console, I get output like this:

    screenshot

  2. for each module alias, find all classes. For the rewrites it is easy, because the full mapping is available (like core/profiler => 'Aoe_Profiler_Block_Profiler'. For all others, you will have to search the file system. For each entry:

    1. convert the class value to a path (Mage_Page_Block => Mage/Page/Block)
    2. search for all files in app/code/*/$path/**. You can use glob() for that.
    3. For the class alias, combine the module alias (e.g. page) and the last part of the file path, replacing / with _ and lowercasing (e.g. Html/Head.php, converted to html_head)
    4. For the class name, either parse the file, or combine the class value (Mage_Page_Block) and the last part of the file path, replacing / with _ (Html_Head)
Related Topic