Opencart 3: how to insert to any page data from own module? - opencart

I have own module with several fields. I want to show this data in contact page and in the header. How to connect this module for exemple to contact page controller?
I have tried connect it like this:
$data['mymodule'] = $this->load->controller('extension/module/mymodule');
But nothing. What I need to do?
PS. My own mudule doesn't have model. Cotroller and language only.

As I thought, it's very easy to do. But I had to figure out exactly how.
First of all, we need to connect the modules model in the controller. Then connect the module that we need by ID. At the end, according to the standard opencart scheme, we specify variables for outputting data from the array.
$this->load->model('setting/module');
$my_variable = $this->model_setting_module->getModule('module id');
$data['any_variable'] = $my_variable['any_variable'];
In the template
{{ any_variable }}

Related

How can I create a custom module to display a calculated value on an already existing page/view?

Trying get started with custom module development on Drupal 8. Don't have previous Drupal dev background. Been reading and watching some materials in the past 2 or so weeks.
I decided to dare doing a little more "complex" other than "Hello World", but I got stuck on how would I actually go about this.
This is what I have so far:
I created a content type for "people". It's very simple, all it has is the person's name and birthdate (date only), so you can use the form to input a few people.
I created a view that displays the list of people, their names and their birthdate. Of course everything so far is using the core admin thing.
Now, I wanted to create a custom module that calculates the person's age (based on the date stored in the DB). It takes today's date, subtracts the person's birthdate, and calculates their age.
Then, with some magic, it returns the age to the render/page output and now this calculated value (the age) will be displayed on the view as well along with the person's name and birthdate.
I assume I would need to "hook into" a place somewhere, where the list of people is returned from the database. Then I loop through the data, get the birthdate, calculate the age, stick the result (age) back into the data, and then the page/view will display this - somehow.
Of course I am already stuck on how would I go about doing this? Where would I need to hook into? With what API? And then of course, I created a view already, but the view doesn't have the "age" field - since that is calculated on the fly. So where and how would I display it?
So many questions...
If anyone would know some tutorial that is similar to this, I'd appreciate it. It's kinda tough getting started with the "custom" side of Drupal.
Thanks for some tips!
without custom programming
You can actually do this without custom coding. For this you can I would use either of the following modules field_token_value or computed_field.
They both create a field without giving the user a textbox to input any value.
The value of such fields are calculated from predefined rules/custom coding/tokens defined per field.
from your custom theme
The easiest way is to do this on the theming layer and not store this value in the database. You have to use hook_preprocess_node in your THEMENAME.theme
function THEMENAME_preprocess_node(&$variables){
//install debug and kint to be able to use
//kint($variables);
$node = $variables["node"];
if($node->getType() == "CONTENTTYPE" && $node->hasField('field_date')){
//get date value something like "2018-09-07T21:35:30"
$date = $node->field_date->value;
//your logic and age calculations
// ...
$age = 35;
//set variable to use in node.html.twig based templates
$variables["person_age"] = $age;
}
}
In order to get more information on what is available install debug module and enable kint (which is part of debug) to be able to see variables using kint function
Than copy node.html.twig from your custom theme (or parent theme or classy core theme) to your THEMENAME/templates folder and rename it node--CONTENTTYPE.html.twig.
In there you can include the variable you just created in proprocess.
person age: {{ person_age }}
Make sure you clear the cache for all those changes to be seen by Drupal.
FOR MORE INFO
To find out what twig templates to override see here
More on twig and getting available variables
Getting more info on which twig template is used can also be obtained by enabling debug mode on previous link.
I would suggest you pick up a book on drupal development to understand all those concepts in a more concrete way.

how to get $formBuilder->getForm() for a webform

I am trying to display my webform in a modal (including error/validation messages inside the modal). I am following this approach for a custom drupal form which is not a webform. I am wondering whether this is possible at all with webforms and in case it is, what I need to take into account to make it run?
The first problem I am having is the following code line from the example's TeacherContactController.php
$modal_form = $this->formBuilder->getForm('Drupal\tl_session\Form\TeacherForm');
When I try to use this in my own controller, I cannot get the webform's proper namespace + id. When I check my webform with devel it says:
[__CLASS__] => Drupal\webform\Entity\Webform
[id] => add_news_webform
But I get a "The form argument Drupal\webform\Entity\Webform\webform-submission-add_news_webform-form is not a valid form" error. I tried many things, but did not succeed.
How can I get the webform with formBuilder and what else am I missing here (like webform specific ajax settings etc.)?
You're trying to hard-code the namespace + ID in a string?
That'd be my first guess as to your problem
try
$modal_form = $this->formBuilder->getForm(Drupal\tl_session\Form\TeacherForm::class);
Or possibly
$modal_form = $this->formBuilder->getForm(\Drupal\tl_session\Form\TeacherForm::class);
If you enable the webform_devel.module you can use the API tab which provides some example code. (/webform/contact/api)
#see \Drupal\webform\WebformSubmissionForm::submitWebformSubmission

How to store a dynamic site-wide variable

I have an html file which is the base,where other html documents extends.Its a static page but i want to have variable in the menu.I don't think it's wise to create a view for it,since i don't intend to let users visit the base alone.So where in my project can I store site-wide dynamic variables that can be called on any page without explicitly stating them in their views.
Thank you in advance.
For user specific variables, use session.
For global constants (not variables!), use settings.py.
For global variables, consider to store it in database so it can be multithreading & multiprocess safe.
I looked around and saw different approaches,but one that doesn't compromise the DRY philosophy the most for me is registering a tag in your project then input it in the base template.Its neater See here https://stackoverflow.com/a/21062774/6629594 for an example
Storage can take any number of places, I put mine in a stats model in the db so you get all the goodness of that (and make it easy to access in views).
I then have a context processor written as so:
#context_processors.py:
def my_custom_context_processor(request):
return {'custom_context_variable1':'foo','custom_context_variable2':'bar'}
Add this to your context processors in settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
...
"my_app.context_processors.ny_custom_context_processor",
)
Provided you use render() to render your templates you can then you can just use:
{{ custom_context_variable1 }}
to return 'foo' in your template. Obviously returning strings is for example only, you can use anything you like so long as your context processor returns a dict.
you can also try using php pages.
Then acces the variable on each page with an include 'file containing the var.php' on every page.
None of this will be visible in the source html as it is only processed on the server side.
If you you would like to try this, mail me and I will send you some sample code.

Can a Custom DataProvider class expose Custom Templates?

I am currently in the process of writing a custom DataProvider. Using the Intergrate External Data documentation.
I've managed to show the external data in the Sitecore back end. However whenever I try to view the data in the items I created, I am getting an error
Null ids are not allowed. <br> Parameter name: displayName
There seems to be precious little on the subject on how to create a custom DataProvider on the Sitecore Developer Network.
The example on their website seems to only show how to import a SINGLE item into a static database. However I am simply trying to merge some items into the hierarchy and I can't find any useful documentation.
It seems that one of your methods that should return an ID doesn't. It might be GetChildIds and/or GetParentId.
Nick Wesselman wrote a good article about it gathering all the information including an example on the Marketplace. I think that is your best start. You can read it here.
Turns out I needed to include at the very least, the Fields->Section->Template in the GetParent method. To be on the safe side I included the Fields/Sections/Templates into my implementations of
GetChildIDs
GetItemDefinition
GetParentID
It wasn't obvious that this was the case, since I had in fact implemented the GetTemplates method correctly, and I had expected that should be enough.

Real example of Ember.js

Please give me real example of emberjs. This example will help me understand how to write in emberjs.
Description of example:
Please open this link first:
http://jsfiddle.net/kwladyka/KhQvu/2/
(this is prototype to better understand what i am talking about)
And then read:
Emberjs download Groups.owner by JSON from json.php?name=groups-owner&user_id=58 and Groups.member from json.php?name=groups-member&user_id=58. Both in Groups.all.
In object Groups i have all values of arrays what i read from JSON. I want example read something like that: owner[2][name].
Then from CreateGroupsSelect i want read Groups.all and create ListBox with values group_id and names name. And when i change select i want change <p>You select {{name}} with id {{id}}</p> and refresh data in other View emberjs objects.
I will be really gratefull for help. I am trying learn emberjs but i totally block with this example.
Ok, so i have many questions :)
1)
How to bind value form input select to attribute in controller
var val = $('select option:selected').val();
Where can i find list of possible ussage like a "select option:selected". Any documentatin for that? Where?
2)
"didInsertElement"
How can i know i should use this? How should i looking in documentation to know that? Why not Ember.Select.Change for that?
3)
Why author use "View", i thought for select i should use class "select"?
4)
Seriously this is my first language when i fill all is so random and i dont have any idea how to know what i can write and where. I fill really stupid with that. Any hint how to understand that?
5)
Ok so step by step...
First of all i want read JSON and prepare data to use.
Organizer.Groups = Ember.Object.extend({
owner: [],
member: [],
all: function(){
var owner = this.get('owner');
var member = this.get('member');
return owner.concat(member);
}.property('owner', 'member'),
});
a) "Object" is good choice for that?
b) I want fill "owner" by array from "json.php?name=groups-owner&user_id=58". How to do that? Did i define "owner" corretly or i should write "ArrayController" or something else for that? Mayby just "Array" or "ArrayProxy"? I am filling so confuse now.
c) How should i initiate load data from JSON? I want do this only one time on the beginning.
PS Sorry if you fill my questions are so stupid but i really dont know answers and i dont hide this :)
Here is an example of how to work with select menu in Ember js
select dropdown with ember
Here is an example of working with routes and outlet helper
Right way to do navigation with Ember
If you use routing make sure you have ember latest, routing is still not released.
Router / StateManager - can't make it work
When using latest emberjs refer to documentation in source code, the documentation in ember site will be for present stable release.
https://github.com/emberjs/ember.js
The links to routing is provided from your fidle title "Router with dynamic routes"
It will be hard to solve your complete problem, give it a try starting with the routing example from above, when you are stuck ask more specific questions, we'll be happy to help.
EDIT : Some of the links are already outdated.