Magento 1.9 – Create a Static Block Using a Custom Module

custommagento-1.9module

In my custom module

app/code/local/Maddyboy/Instagramapi/data/instagramapi_setup/data-upgrade-0.1.0-0.1.1.php

<?php
$content = 'BLOCK CONTENT HERE';
//if you want one block for each store view, get the store collection
$stores = Mage::getModel('core/store')->getCollection()->addFieldToFilter('store_id', array('gt'=>0))->getAllIds();
//if you want one general block for all the store viwes, uncomment the line below
$stores = array(0);
foreach ($stores as $store){
    $block = Mage::getModel('cms/block');
    $block->setTitle('Block title here');
    $block->setIdentifier('block_identifier_here');
    $block->setStores(array($store));
    $block->setIsActive(1);
    $block->setContent($content);
    $block->save();
}

app/code/local/Maddyboy/Instagramapi/etc/config.xml

<Maddyboy_Instagramapi>
      <version>0.1.1</version> // changed to 0.1.1
</Maddyboy_Instagramapi>

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

<?xml version="1.0"?>   
<layout version="0.1.1"> 

Still, static block is not shown in the admin panel.

config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <Maddyboy_Instagramapi>
      <version>0.1.1</version>
    </Maddyboy_Instagramapi>
  </modules>
  <frontend>
    <routers>
      <instagramapi>
        <use>standard</use>
          <args>
            <module>Maddyboy_Instagramapi</module>
            <frontName>instagramapi</frontName>
          </args>
      </instagramapi>
    </routers>
        <layout>
          <updates>
            <instagramapi>
              <file>instagramapi.xml</file>
            </instagramapi>
          </updates>
        </layout>
  </frontend>
  <global>
    <helpers>
      <instagramapi>
        <class>Maddyboy_Instagramapi_Helper</class>
      </instagramapi>
    </helpers>
    <blocks>
      <instagramapi>
        <class>Maddyboy_Instagramapi_Block</class>
      </instagramapi>
    </blocks>
    <models>
      <instagramapi>
        <class>Maddyboy_Instagramapi_Model</class>
        <resourceModel>instagramapi_mysql4</resourceModel>
      </instagramapi>
    </models>
     <events>
            <controller_front_init_routers>
                <observers>
                    <Maddyboy_Instagramapi_model_observer>
                        <type>singleton</type>
                        <class>Maddyboy_Instagramapi_Model_Observer</class>
                        <method>checkForConfigRequest</method>
                    </Maddyboy_Instagramapi_model_observer>
                </observers>
            </controller_front_init_routers>
        </events>

  </global>
  <admin>
    <routers>
      <instagramapi>
        <use>admin</use>
        <args>
          <module>Maddyboy_Instagramapi</module>
          <frontName>admin_instagramapi</frontName>
        </args>
      </instagramapi>
    </routers>
  </admin>
  <adminhtml>
    <menu>
      <instagramapi module="instagramapi">
        <title>Instagramapi</title>
        <sort_order>100</sort_order>
        <children>
          <instagramapibackend module="instagramapi">
            <title>Backend Page Title</title>
            <sort_order>0</sort_order>
            <action>admin_instagramapi/adminhtml_instagramapibackend</action>
          </instagramapibackend>
        </children>
      </instagramapi>
    </menu>
    <acl>
      <resources>
        <all>
          <title>Allow Everything</title>
        </all>
        <admin>
          <children>
            <instagramapi translate="title" module="instagramapi">
              <title>Instagramapi</title>
              <sort_order>1000</sort_order>
              <children>
          <instagramapibackend translate="title">
            <title>Backend Page Title</title>
          </instagramapibackend>
              </children>
            </instagramapi>
          </children>
        </admin>
      </resources>
    </acl>
    <layout>
      <updates>
        <instagramapi>
          <file>instagramapi.xml</file>
        </instagramapi>
      </updates>
    </layout>
  </adminhtml>    
    <crontab>
        <jobs>            
            <instagramapi_instagramapicron>
                <schedule><cron_expr>*/5 * * * *</cron_expr></schedule>
                <run><model>instagramapi/cron::Instagramapicron</model></run>
            </instagramapi_instagramapicron>
        </jobs>
    </crontab>
</config> 

UPDATE
Custom static block is created now, is there any way through which i can append the created block to some reference like head

In controller we can append like

    $block = $this->getLayout()->createBlock(
'Mage_Core_Block_Template',
'my_block_name_here',
array('template' => 'instagramapi/apislider.phtml')
);

$this->getLayout()->getBlock('head')->append($block);

but how to do this in my situation.

Best Answer

you are missing below code in your config.xml, add below code under global tag

<resources>
    <instagramapi_setup>
        <setup>
            <module>Maddyboy_Instagramapi</module>
        </setup>
        <connection>
            <use>core_setup</use>
        </connection>
    </instagramapi_setup>
</resources>

Also if you don't have any previous sql script in your module then your file name should be mysql4-install-0.1.0.php here(0.1.0 is your version)

Related Topic