1

I'm trying to write a module with a custom block that inject some javascript to the page head. no matter what I do I keep getting "invalid block type" error.

Note: I went over all the questions here about "invalid block type" and none of them worked / were relevant.

my extension files look like this:

enter image description here

my config file (Packname/Core/etc/config.xml):

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <Packname_Core>
      <version>0.1.0</version>
    </Packname_Core>
  </modules>

  <global>  
    <blocks>
      <packname_core>
        <class>Packname_Core_Block</class>
      </packname_core>
    </blocks>
  </global>

  <frontend>
    <layout>
      <updates>
        <packname>
          <file>packname_core.xml</file>
        </packname>
      </updates>
    </layout>
  </frontend>
</config> 

my layout file (design\frontend\base\default\layout\packname_core.xml):

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
    <default>
        <reference name="head">
                <block type="packname_core/injectjs" name="packnameJs" as="packnameJs" template="packname/injectjs.phtml" />
        </reference>
    </default>
</layout>

and my block file (Packname\Core\Block\Injectjs.php):

<?php

class Packname_Core_Block_Injectjs extends Mage_Core_Block_Template
{
    public function getPacknameJsUrl()
    {
        return Mage::helper('packname_core/config')->getJsUrl();
    }
}

now the error I'm getting looks like this:

exception 'Mage_Core_Exception' with message 'Invalid block type: Packname_Core_Block_Injectjs' in C:\Bitnami\magento-1.9.2.1-1\apps\magento\htdocs\app\Mage.php:595 

the name Packname_Core_Block_Injectjs seems correct, I already double checked uppercase/lowercase issues, and I can't figure out what's wrong.. everything seem in place.

note that if I set block type to something like "core/html" I no longer get the error, but then offcourse I can't access my block functions.

any help will be greatly appreciated! thanks,

  • <codePool>community</codePool> did you check this? – Qaisar Satti Oct 19 '15 at 12:38
1

i tried this code and could not find the error check your configuration

app/etc/modules/Packname_Core.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Packname_Core>
      <active>true</active>
      <codePool>community</codePool>
      <version>0.1.0</version>
    </Packname_Core>
  </modules>
</config>

app/code/community/Packname/Core/etc/config.xml

<?xml version="1.0" encoding="UTF-8"?>
<config>
  <modules>
    <Packname_Core>
      <version>0.1.0</version>
    </Packname_Core>
  </modules>

  <global>  
    <blocks>
      <packname_core>
        <class>Packname_Core_Block</class>
      </packname_core>
    </blocks>
  </global>

  <frontend>

            <routers>
                <packname_core>
                    <use>standard</use>
                    <args>
                        <module>Packname_Core</module>
                        <frontName>packname</frontName>
                    </args>
                </packname_core>
            </routers>

    <layout>
      <updates>
        <packname>
          <file>packname_core.xml</file>
        </packname>
      </updates>
    </layout>
  </frontend>
</config> 

app/design/frontend/base/default/layout/packname_core.xml

<?xml version="1.0" encoding="UTF-8"?>
<layout version="0.1.0">
    <default>
        <packname_index_index>
        <reference name="content">
                <block type="packname_core/injectjs" name="packnameJs" as="packnameJs" template="packname/injectjs.phtml" />
        </reference>
    </packname_index_index>
    </default>
</layout>

app/code/community/Packname/Core/controllers/IndexController.php

class Packname_Core_IndexController extends Mage_Core_Controller_Front_Action
{

    public function IndexAction()
    { 

        $this->loadLayout()
                   ;                 
    $this->renderLayout();

    }
}

app/code/community/Packname/Core/Block/Injectjs.php

class Packname_Core_Block_Injectjs extends Mage_Core_Block_Template
{
    public function getPacknameJsUrl()
    {
        return Mage::helper('packname_core/config')->getJsUrl();
    }
}

app/design/frontend/base/default/template/packname/injectjs.phtml

<div>adaskd;asdask;lad gfdfgfd </div>
  • wow you took a lot of effort to help me out, thanks! I compared my code to your example and it looks the same except for the "<packname_index_index>" tag in the layout file, since I need this layout to update all pages, not just the router I define. error still occur. is the <packname_index_index> mandatory? I want it to affect all pages. thanks – Ronen Ness Oct 19 '15 at 13:56
  • No this is for your page for test but no errors disabled the cache and remove var/cache then check – Qaisar Satti Oct 19 '15 at 14:00
  • did it, error still happens. is there a chance you can upload your extension zip somewhere and post the link? PS I have magento 1.9.2.1, if that matters. thanks. – Ronen Ness Oct 19 '15 at 14:14
  • Try to replace all files with my files and then and head will no add phtml file you want add open page.xml of any theme and check how they are adding it – Qaisar Satti Oct 19 '15 at 14:17
  • did that, I don't see error but I don't see the "daskd;asdask;lad gfdfgf" div either (obviously in /packname url). are you sure your code works? do you see the div? thanks, (PS: I added "<?php" to begining of all files, I assume you just left them out since it doesn't work without it) – Ronen Ness Oct 19 '15 at 14:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.