Jump to content

Create CMS page programmatically


Recommended Posts

Hello,

I like to ask you how to create page programmatically from custom module?

I know how to create controller and assign .tpl to this controller

My idea: customer clicks "create" button from BO module page and it creates the page with url and page details he choosed.

 

Thanks a lot!

Edited by Amantha Bill (see edit history)
Link to comment
Share on other sites

On 5/2/2020 at 7:21 AM, Guest said:

I already gave you the instructions.

 


Creating a module is not a problem.

Thanks, but it is not the same problem. I need to create "CMS page" with no controller

Something like:

$cms = new CMS //to create page automatically

$cms->link_rewrite; //add url link, etc
$cms->add();

Do you know something about it? I saw there is a CMS class, but I didn't find the instructions about it

 

Edited by Amantha Bill (see edit history)
Link to comment
Share on other sites

35 minutes ago, Guest said:

Topic name = Create page programmatically

change to Create CMS page programmatically 😉

Thanks, did it.

As I understood, I need to create controller than class to create cms pages.

I need to create something like "blog" with news of web-site.

Can you help me?

Link to comment
Share on other sites

It's not so easy.
SQL queries must be used.
It is impossible to write the whole code here.
Just look at the tables in the database and then everything will be clear.

Např.:

// table ps_cms //
// cms position
$cms_position = Db::getInstance()->getValue('SELECT position FROM '._DB_PREFIX_.'cms ORDER BY position DESC') + 1;

// cms category
$cms_category = 1;

// cms active
$cms_active = 1;

// cms indexation
$cms_indexation = 1;

// create cms id
$cms = Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'cms (id_cms_category, position, active, indexation) VALUES ($cms_category, $cms_position, $cms_active, $cms_indexation)');

// inserted cms id
$cms_id = Db::getInstance()->Insert_ID();

// table ps_cmas_lang //
// cms for multistore
$cms_shops = array(1,2);

// cms for actual store
$cms_shop = (int)Context::getContext()->shop->id;

// cms languages for multistore
$cms_langs_stores = Language::getLanguages(true);

// cms languages for current store 
$cms_langs = Language::getLanguages(true, $this->context->shop->id);

// cms current language
$cms_lang = (int)Context::getContext()->language->id;

$cms_meta_title = 'your metatitle';
$cms_seo_head_titile = 'your head seo titile';
$cms_meta_description = 'your meta description';
$cms_meta_keywords = 'your,meta,keywords';
$cms_content = 'your html content';
$cms_link_rewrite = 'your-link-rewrite';


// create cms page content
Db::getInstance()->Execute("INSERT INTO "._DB_PREFIX_."cms_lang (id_cms, id_lang, id_shop, meta_title, head_so_title, meta_description, meta_keywords, content, link_rewrite) 
VALUES ($cms_id, $cms_lang, $cms_shop, '".$cms_meta_title."', '".$cms_seo_head_titile."', '".$cms_meta_description."', '".$cms_meta_keywords."', '".$cms_content."', '".$cms_link_rewrite."')");


// associate created cms page to shop
Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'cms_shop (id_cms, id_shop) VALUES ($cms_id,$cms_shop); 

If you want to add more languages and more shops, you have to use foreach.

Edited by Guest (see edit history)
Link to comment
Share on other sites

15 minutes ago, Guest said:

It's not so easy.
SQL queries must be used.
It is impossible to write the whole code here.
Just look at the tables in the database and then everything will be clear.

Např.:


// table ps_cms //
// cms position
$cms_position = Db::getInstance()->getValue('SELECT position FROM '._DB_PREFIX_.'cms ORDER BY position DESC') + 1;

// cms category
$cms_category = 1;

// cms active
$cms_active = 1;

// cms indexation
$cms_indexation = 1;

// create cms id
$cms = Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'cms (id_cms_category, position, active, indexation) VALUES ($cms_category, $cms_position, $cms_active, $cms_indexation)');

// inserted cms id
$cms_id = Db::getInstance()->Insert_ID();

// table ps_cmas_lang //
// cms for multistore
$cms_shops = array(1,2);

// cms for actual store
$cms_shop = (int)Context::getContext()->shop->id;

// cms languages for multistore
$cms_langs_stores = Language::getLanguages(true);

// cms languages for current store 
$cms_langs = Language::getLanguages(true, $this->context->shop->id);

// cms current language
$cms_lang = (int)Context::getContext()->language->id;

$cms_meta_title = 'your metatitle';
$cms_seo_head_titile = 'your head seo titile';
$cms_meta_description = 'your meta description';
$cms_meta_keywords = 'your,meta,keywords';
$cms_content = 'your html content';
$cms_link_rewrite = 'your-link-rewrite';


// create cms page content
Db::getInstance()->Execute("INSERT INTO "._DB_PREFIX_."cms_lang (id_cms, id_lang, id_shop, meta_title, head_so_title, meta_description, meta_keywords, content, link_rewrite) 
VALUES ($cms_id, $cms_lang, $cms_shop, '".$cms_meta_title."', '".$cms_seo_head_titile."', '".$cms_meta_description."', '".$cms_meta_keywords."', '".$cms_content."', '".$cms_link_rewrite."')");


// associate created cms page to shop
Db::getInstance()->Execute('INSERT INTO '._DB_PREFIX_.'cms_shop (id_cms, id_shop) VALUES ($cms_id,$cms_shop); 

If you want to add more languages and more shops, you have to use foreach.

Thanks a lot! I modified your code already from your previous post for my needs.

Thanks a lot for your help, I think soon I will create full tutorial how to create cms pages from module.

Edited by Amantha Bill (see edit history)
Link to comment
Share on other sites

11 minutes ago, Guest said:

Will your demonstration look something like this? 😁

obrazek.thumb.png.298bde986648073ad0516f16aaf81cd1.png

Yes, but how did you get "custom CSS" field like this?

I have found only next lines:

array(
                        'col' => 3,
                        'type' => 'text',
                        'prefix' => '<i class="icon icon-envelope"></i>',
                        'desc' => $this->l('SEO HEAD'),
                        'name' => 'BL_SEO_HEAD',
                        'label' => $this->l('Seo head'),
                    ),

 

Link to comment
Share on other sites

$fields_form = array(
            'form' => array(
                'tinymce' => true,
                
                'legend' => array(
                    'title' => $this->l('Settings custom page'),
                    'icon' => 'icon-cogs'
                ),
                'input' => array(
                    
                    ...
...

// tinymce editor
array(
                        'type' => 'textarea',
                        'label' => $this->l('Page content'),
                        'name' => '_content',
                        'lang' => false,
                        'autoload_rte' => true,
                        'cols' => 50,
                        'rows' => 20
                    ),
...
...
// normal editor
array(
                        'type' => 'textarea',
                        'label' => $this->l('Custom CSS'),
                        'name' => '_css',
                        'lang' => false,
                        'autoload_rte' => false,
                        'cols' => 50,
                        'rows' => 5
                    ),
...
...

 

Edited by Guest (see edit history)
Link to comment
Share on other sites

6 minutes ago, Guest said:

$fields_form = array(
            'form' => array(
                'tinymce' => true,
                
                'legend' => array(
                    'title' => $this->l('Settings custom page'),
                    'icon' => 'icon-cogs'
                ),
                'input' => array(
                    
                    ...
...

// tinymce editor
array(
                        'type' => 'textarea',
                        'label' => $this->l('Page content'),
                        'name' => '_content',
                        'lang' => false,
                        'autoload_rte' => true,
                        'cols' => 50,
                        'rows' => 20
                    ),
...
...
// normal editor
array(
                        'type' => 'textarea',
                        'label' => $this->l('Custom CSS'),
                        'name' => '_css',
                        'lang' => false,
                        'autoload_rte' => false,
                        'cols' => 50,
                        'rows' => 5
                    ),
...
...

 

thanks, perfect!

Link to comment
Share on other sites

  • 1 month later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...