Post by gabba0000 » Fri Jan 22, 2021 3:05 am

Hi There,

The company I work for is having a website developed using Opencart. I am a apprentice and my boss has asked me to create a "Hello World" module, where we can simply present text to users, I am new to Opencart so we are starting with something simple. I know that there is a module HTML for presenting text on the front end, but for the purposes of understanding modules, this is what my boss assigned me to do.

I have created files in the following directories:

/admin/language/en-gb/extension/module/helloworld.php
/admin/controller/extension/module/helloworld.php
/admin/view/template/extension/module/helloworld.twig
/catalog/controller/extension/module/helloworld.php
/catalog/view/theme/default/template/extension/modue/helloworld.twig

From the admin side, you can insert a header and text in a text field which would then be displayed the user once my helloworld module has been introduced to a layout. However, it is not updating / being shown on the front end after adding it to my home page layout.

It is inserting into the oc_module as it should and the module has been enabled. I have also cleared my browser history. I have also refreshed the Theme and SASS on the dashboard developer settings.

I know it will be something really obvious but as i am new to this i cannot seem to figure it out.

Thanks,

Newbie

Posts

Joined
Mon Nov 30, 2020 1:03 am

Post by sw!tch » Fri Jan 22, 2021 3:52 am

If you want help provide code or post in the commercial section.

Post your OC version.

Full Stack Web Developer :: Send a PM for Custom Work.
Backup and learn how to recover before you make any changes!


Active Member

Posts

Joined
Sat Apr 28, 2012 2:32 pm
Location - USA

Post by mikeinterserv » Fri Jan 22, 2021 4:04 am

gabba0000 wrote:
Fri Jan 22, 2021 3:05 am
Hi There,
However, it is not updating / being shown on the front end after adding it to my home page layout.
are you sure you are ADDING it and not just selecting it and saving.
Select it - then press ADD THEN save it

Active Member

Posts

Joined
Thu May 28, 2020 6:55 am
Location - Wales

Post by xxvirusxx » Fri Jan 22, 2021 4:46 pm

gabba0000 wrote:
Fri Jan 22, 2021 3:05 am
but for the purposes of understanding modules
By you or by the boss?

Extensions converted by me | Custom 3.0.3.7 | Buy me a beer


User avatar
Expert Member

Posts

Joined
Tue Jul 17, 2012 10:35 pm
Location - România

Post by gabba0000 » Fri Jan 22, 2021 5:06 pm

I press the plus symbol and then select my module. Followed by saving the layout change.

Newbie

Posts

Joined
Mon Nov 30, 2020 1:03 am

Post by gabba0000 » Fri Jan 22, 2021 5:07 pm

xxvirusxx wrote:
Fri Jan 22, 2021 4:46 pm
gabba0000 wrote:
Fri Jan 22, 2021 3:05 am
but for the purposes of understanding modules
By you or by the boss?
It is for me to start understanding how modules work on OpenCart.

Newbie

Posts

Joined
Mon Nov 30, 2020 1:03 am

Post by xxvirusxx » Fri Jan 22, 2021 5:14 pm

If you don't post code or files nobody will able to help you.

Extensions converted by me | Custom 3.0.3.7 | Buy me a beer


User avatar
Expert Member

Posts

Joined
Tue Jul 17, 2012 10:35 pm
Location - România

Post by gabba0000 » Fri Jan 22, 2021 5:38 pm

xxvirusxx wrote:
Fri Jan 22, 2021 5:14 pm
If you don't post code or files nobody will able to help you.
Please see below:

/admin/language/en-gb/extension/module/helloworld.php

Code: Select all

<?php
// Heading
	$_['heading_title']     = 'Hello World Content';

// Text
	$_['text_extension']    = 'Extensions';
	$_['text_success']      = 'Success: You have modified Hello World Content module!';
	$_['text_edit']         = 'Edit Hello World Content Module';

// Entry
	$_['entry_name']        = 'Module Name';
	$_['entry_title']       = 'Heading Title';
	$_['entry_status']      = 'Status';

// Error
	$_['error_permission']  = 'Warning: You do not have permission to modify Hello World Content module!';
	$_['error_name']        = 'Module Name must be between 3 and 64 characters!';
	
/admin/controller/extension/module/helloworld.php

Code: Select all

<?php
class ControllerExtensionModuleHelloworld extends Controller {

	private $error = array();
	
	public function index()	{

		$this->load->language('extension/module/helloworld');

		$this->document->setTitle($this->language->get('heading_title'));

		$this->load->model('setting/module');

		if (($this->request->server['REQUEST_METHOD'] == 'POST') && $this->validate()) {
			
			if (!isset($this->request->get['module_id'])) {
				
				$this->model_setting_module->addModule('helloworld', $this->request->post);

			} else {
				
				$this->model_setting_module->editModule($this->request->get['module_id'], $this->request->post);
				
			}
	
			$this->session->data['success'] = $this->language->get('text_success');

			$this->response->redirect($this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module', true));
		}
		
		if (isset($this->error['warning'])) {

			$data['error_warning'] = $this->error['warning'];

		} else {
		
			$data['error_warning'] = '';

		}
		
		if (isset($this->error['name'])) {

			$data['error_name'] = $this->error['name'];

		} else {

			$data['error_name'] = '';

		}

		$data['breadcrumbs'] = array();

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_home'),
			'href' => $this->url->link('common/dashboard', 'user_token=' . $this->session->data['user_token'], true)
		);

		$data['breadcrumbs'][] = array(
			'text' => $this->language->get('text_extension'),
			'href' => $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module', true)
		);

		if (!isset($this->request->get['module_id'])) {

			$data['breadcrumbs'][] = array(
				'text' => $this->language->get('heading_title'),
				'href' => $this->url->link('extension/module/helloworld', 'user_token=' . $this->session->data['user_token'], true)
			);

		} else {

			$data['breadcrumbs'][] = array(
				'text' => $this->language->get('heading_title'),
				'href' => $this->url->link('extension/module/helloworld', 'user_token=' . $this->session->data['user_token'] . '&module_id=' . $this->request->get['module_id'], true)
			);

		}

		if (!isset($this->request->get['module_id'])) {

			$data['action'] = $this->url->link('extension/module/helloworld', 'user_token=' . $this->session->data['user_token'], true);

		} else {

			$data['action'] = $this->url->link('extension/module/helloworld', 'user_token=' . $this->session->data['user_token'] . '&module_id=' . $this->request->get['module_id'], true);
			
		}

		$data['cancel'] = $this->url->link('marketplace/extension', 'user_token=' . $this->session->data['user_token'] . '&type=module', true);
		
		if (isset($this->request->get['module_id']) && ($this->request->server['REQUEST_METHOD'] != 'POST')) {

			$module_info = $this->model_setting_module->getModule($this->request->get['module_id']);

		}

		if (isset($this->request->post['name'])) {

			$data['name'] = $this->request->post['name'];

		} elseif (!empty($module_info)) {

			$data['name'] = $module_info['name'];

		} else {

			$data['name'] = '';

		}

		if (isset($this->request->post['module_description'])) {

			$data['module_description'] = $this->request->post['module_description'];

		} elseif (!empty($module_info)) {

			$data['module_description'] = $module_info['module_description'];

		} else {

			$data['module_description'] = array();
		
		}
                
		$this->load->model('localisation/language');

		$data['languages'] = $this->model_localisation_language->getLanguages();

		if (isset($this->request->post['status'])) {

			$data['status'] = $this->request->post['status'];

		} elseif (!empty($module_info)) {

			$data['status'] = $module_info['status'];

		} else {

			$data['status'] = '';

		}

		$data['header'] = $this->load->controller('common/header');
		$data['column_left'] = $this->load->controller('common/column_left');
		$data['footer'] = $this->load->controller('common/footer');

		$this->response->setOutput($this->load->view('extension/module/helloworld', $data));
	}


	protected function validate()
	{

		if (!$this->user->hasPermission('modify', 'extension/module/helloworld')) {

			$this->error['warning'] = $this->language->get('error_permission');

		}

		if ((utf8_strlen($this->request->post['name']) < 3) || (utf8_strlen($this->request->post['name']) > 64)) {

			$this->error['name'] = $this->language->get('error_name');

		}

		return !$this->error;
	}

	public function install()
	{

		$this->load->model('setting/setting');
		$this->model_setting_setting->editSetting('module_helloworld', ['module_helloworld_status' => 1]);

	}

	public function uninstall()
	{

		$this->load->model('setting/setting');
		$this->model_setting_setting->deleteSetting('module_helloworld');

	}
}

/admin/view/template/extension/module/helloworld.twig

Code: Select all


{{ header }}{{ column_left }}
<div id="content">
  <div class="page-header">
    <div class="container-fluid">
      <div class="pull-right">
        <button type="submit" form="form-module" data-toggle="tooltip" title="{{ button_save }}" class="btn btn-primary"><i class="fa fa-save"></i></button>
        <a href="{{ cancel }}" data-toggle="tooltip" title="{{ button_cancel }}" class="btn btn-default"><i class="fa fa-reply"></i></a></div>
      <h1>{{ heading_title }}</h1>
      <ul class="breadcrumb">
        {% for breadcrumb in breadcrumbs %}
        <li><a href="{{ breadcrumb.href }}">{{ breadcrumb.text }}</a></li>
        {% endfor %}
      </ul>
    </div>
  </div>
  <div class="container-fluid">
    {% if error_warning %}
    <div class="alert alert-danger alert-dismissible"><i class="fa fa-exclamation-circle"></i> {{ error_warning }}
      <button type="button" class="close" data-dismiss="alert">&times;</button>
    </div>
    {% endif %}
    <div class="panel panel-default">
      <div class="panel-heading">
        <h3 class="panel-title"><i class="fa fa-pencil"></i> {{ text_edit }}</h3>
      </div>
      <div class="panel-body">
        <form action="{{ action }}" method="post" enctype="multipart/form-data" id="form-module" class="form-horizontal">
          <div class="form-group">
            <label class="col-sm-2 control-label" for="input-name">{{ entry_name }}</label>
            <div class="col-sm-10">
              <input type="text" name="name" value="{{ name }}" placeholder="{{ entry_name }}" id="input-name" class="form-control" />
              {% if error_name %}
              <div class="text-danger">{{ error_name }}</div>
              {% endif %}
            </div>
          </div>
          <div class="tab-pane">
            <ul class="nav nav-tabs" id="language">
              {% for language in languages %}
              <li><a href="#language{{ language.language_id }}" data-toggle="tab"><img src="language/{{ language.code }}/{{ language.code }}.png" title="{{ language.name }}" /> {{ language.name }}</a></li>
              {% endfor %}
            </ul>
            <div class="tab-content">
              {% for language in languages %}
              <div class="tab-pane" id="language{{ language.language_id }}">
                <div class="form-group">
                  <label class="col-sm-2 control-label" for="input-title{{ language.language_id }}">{{ entry_title }}</label>
                  <div class="col-sm-10">
                    <input type="text" name="module_description[{{ language.language_id }}][title]" placeholder="{{ entry_title }}" id="input-heading{{ language.language_id }}" value="{{ module_description[language.language_id] ? module_description[language.language_id].title }}" class="form-control" />
                  </div>
                </div>
                <div class="form-group">
                  <label class="col-sm-2 control-label" for="input-description{{ language.language_id }}">{{ entry_description }}</label>
                  <div class="col-sm-10">
                    <textarea name="module_description[{{ language.language_id }}][description]" placeholder="{{ entry_description }}" id="input-description{{ language.language_id }}" data-toggle="summernote" class="form-control">{{ module_description[language.language_id] ? module_description[language.language_id].description }}</textarea>
                  </div>
                </div>
              </div>
              {% endfor %}
            </div>
          </div>
          <div class="form-group">
            <label class="col-sm-2 control-label" for="input-status">{{ entry_status }}</label>
            <div class="col-sm-10">
              <select name="status" id="input-status" class="form-control">
                {% if status %}
                <option value="1" selected="selected">{{ text_enabled }}</option>
                <option value="0">{{ text_disabled }}</option>
                {% else %}
                <option value="1">{{ text_enabled }}</option>
                <option value="0" selected="selected">{{ text_disabled }}</option>
                {% endif %}
              </select>
            </div>
          </div>
        </form>
      </div>
    </div>
  </div>
  <link href="view/javascript/codemirror/lib/codemirror.css" rel="stylesheet" />
  <link href="view/javascript/codemirror/theme/monokai.css" rel="stylesheet" />
  <script type="text/javascript" src="view/javascript/codemirror/lib/codemirror.js"></script> 
  <script type="text/javascript" src="view/javascript/codemirror/lib/xml.js"></script> 
  <script type="text/javascript" src="view/javascript/codemirror/lib/formatting.js"></script> 
    
  <script type="text/javascript" src="view/javascript/summernote/summernote.js"></script>
  <link href="view/javascript/summernote/summernote.css" rel="stylesheet" />
  <script type="text/javascript" src="view/javascript/summernote/summernote-image-attributes.js"></script>
  <script type="text/javascript" src="view/javascript/summernote/opencart.js"></script>
  <script type="text/javascript"><!--
$('#language a:first').tab('show');
//--></script></div>
{{ footer }}

/catalog/controller/extension/module/helloworld.php

Code: Select all

<?php
class ControllerExtensionModuleHelloworld extends Controller {
	public function index($setting) {
		if (isset($setting['module_description'][$this->config->get('config_language_id')])) {
			$data['heading_title'] = html_entity_decode($setting['module_description'][$this->config->get('config_language_id')]['title'], ENT_QUOTES, 'UTF-8');
			$data['helloworld'] = html_entity_decode($setting['module_description'][$this->config->get('config_language_id')]['description'], ENT_QUOTES, 'UTF-8');

			return $this->load->view('extension/module/html', $data);
		}
	}
}

/catalog/view/theme/default/template/extension/modue/helloworld.twig

Code: Select all

<div>

  {% if html %}
      <h2>{{ html }}</h2>
  {% endif %}
</div>


Newbie

Posts

Joined
Mon Nov 30, 2020 1:03 am

Post by sw!tch » Fri Jan 22, 2021 5:44 pm

Untested, but quickly looking your view appears wrong...

Code: Select all

class ControllerExtensionModuleHelloworld extends Controller {
   .....
       return $this->load->view('extension/module/html', $data);
with

Code: Select all

class ControllerExtensionModuleHelloworld extends Controller {
   .....
       return $this->load->view('extension/module/helloworld', $data);

Full Stack Web Developer :: Send a PM for Custom Work.
Backup and learn how to recover before you make any changes!


Active Member

Posts

Joined
Sat Apr 28, 2012 2:32 pm
Location - USA

Post by xxvirusxx » Fri Jan 22, 2021 5:49 pm

And remove both function (install, uninstall) from controller.

Extensions converted by me | Custom 3.0.3.7 | Buy me a beer


User avatar
Expert Member

Posts

Joined
Tue Jul 17, 2012 10:35 pm
Location - România

Post by gabba0000 » Fri Jan 22, 2021 5:50 pm

sw!tch wrote:
Fri Jan 22, 2021 5:44 pm
Untested, but quickly looking your view appears wrong...

Code: Select all

class ControllerExtensionModuleHelloworld extends Controller {
   .....
       return $this->load->view('extension/module/html', $data);
with

Code: Select all

class ControllerExtensionModuleHelloworld extends Controller {
   .....
       return $this->load->view('extension/module/helloworld', $data);
Thanks, as that was a mistake, I have changed that.

Still isn't showing on the front end

Newbie

Posts

Joined
Mon Nov 30, 2020 1:03 am

Post by sw!tch » Fri Jan 22, 2021 5:51 pm

Also not seeing html defined in your controller.

Code: Select all

 {% if html %}
      <h2>{{ html }}</h2>
  {% endif %}
Maybe you meant your twig to be?

Code: Select all

  {% if helloworld %}
      <h2>{{ helloworld }}</h2>
  {% endif %}

Full Stack Web Developer :: Send a PM for Custom Work.
Backup and learn how to recover before you make any changes!


Active Member

Posts

Joined
Sat Apr 28, 2012 2:32 pm
Location - USA

Post by gabba0000 » Fri Jan 22, 2021 6:08 pm

sw!tch wrote:
Fri Jan 22, 2021 5:51 pm
Also not seeing html defined in your controller.

Code: Select all

 {% if html %}
      <h2>{{ html }}</h2>
  {% endif %}
Maybe you meant your twig to be?

Code: Select all

  {% if helloworld %}
      <h2>{{ helloworld }}</h2>
  {% endif %}
Thanks for that, I've done this. But still no luck... is there anything else I've done wrong?

Newbie

Posts

Joined
Mon Nov 30, 2020 1:03 am

Post by sw!tch » Fri Jan 22, 2021 6:11 pm

Deactivate the module and then activate it again. Fill out some test content.

Then go back to design view where you want it to appear and ensure you are clicking the plus sign before saving.

Theme used? OC version?

Full Stack Web Developer :: Send a PM for Custom Work.
Backup and learn how to recover before you make any changes!


Active Member

Posts

Joined
Sat Apr 28, 2012 2:32 pm
Location - USA

Post by gabba0000 » Fri Jan 22, 2021 6:19 pm

sw!tch wrote:
Fri Jan 22, 2021 6:11 pm
Deactivate the module and then activate it again. Fill out some test content.

Then go back to design view where you want it to appear and ensure you are clicking the plus sign before saving.

Theme used? OC version?
OC Version: Version 3.0.3.2
Theme: Journal3

Newbie

Posts

Joined
Mon Nov 30, 2020 1:03 am

Post by sw!tch » Fri Jan 22, 2021 6:25 pm

gabba0000 wrote:
Fri Jan 22, 2021 6:19 pm
OC Version: Version 3.0.3.2
Theme: Journal3
Explains it.

Full Stack Web Developer :: Send a PM for Custom Work.
Backup and learn how to recover before you make any changes!


Active Member

Posts

Joined
Sat Apr 28, 2012 2:32 pm
Location - USA

Post by xxvirusxx » Fri Jan 22, 2021 6:26 pm

You need to add that module to journal 3 layout.

Or switch to Default theme and see if work.

Extensions converted by me | Custom 3.0.3.7 | Buy me a beer


User avatar
Expert Member

Posts

Joined
Tue Jul 17, 2012 10:35 pm
Location - România

Post by gabba0000 » Fri Jan 22, 2021 6:47 pm

xxvirusxx wrote:
Fri Jan 22, 2021 6:26 pm
You need to add that module to journal 3 layout.

Or switch to Default theme and see if work.
Spot on thank you. It now does as I wanted it to. Thanks everyone!

Newbie

Posts

Joined
Mon Nov 30, 2020 1:03 am
Who is online

Users browsing this forum: No registered users and 9 guests