How do I use variables in a different place? - php

I'm trying to use a variable in a different place but it doesn't seem to be working.
Basically the idea that we had is to use the data you assign in the admin panel for your contact info that will be displayed on the contact page onto our footer. But whenever we try to use {{ location.adress }} in our footer.twig file it returns nothing.

To insert anything in view in .twig file you have to determine it in corresponding controller file, like #K. B. said.
In this particular case open /catalog/controller/common/footer.php
Find
$data['newsletter'] = $this->url->link('account/newsletter', '', true);
Add after
$data['address'] = nl2br($this->config->get('config_address'));
Now open /catalog/view/theme/YOUR_THEME/template/common/footer.twig
And place anywhere you need
{{ address }}
Than clear TWIG cache. Done. Now the address from the settings is in your footer.

If you wish to retrieve some data in your template, you must declare that data in corresponding controller file. For example if need retrieve {{ address }} it should be declared $data['address'] = 'data_retrieved_from_db'; For admin and for catalog are different files.

Related

Export a list of users in OctoberCMS to CSV

I have read tutorial about using the import and export functionality in OctoberCMS.
But with the rainlab-users plugin, these guidelines don't work. I also tried to install the plugin vojtasvoboda-userimportexport, but it exports all users, not filtered ones.
class Users extends \RainLab\User\Controllers\Users
{
public $implement = [
'Backend.Behaviors.FormController',
'Backend.Behaviors.ListController',
'Backend.Behaviors.ImportExportController',
];
....
}
When I added this code into Users.php controller, I got an error:
Class .....\User\Controllers\Users has already been extended with Backend\Behaviors\ImportExportController.
When I try export without code above, I got error:
Call to undefined method October\Rain\Database\QueryBuilder::export()
I think its little difficult as we are not having access to user plugin toolbar to add button so.
But YES we can do it, we need to try little harder :) lets start
End result
To add export button we need to Extend rainlab.user plugin. So from your own plugin you need to it.
1. Adding Extension code to your plugin's Boot method
class Plugin extends PluginBase
{
use \System\Traits\ConfigMaker; // trait to read config
public function boot() {
\RainLab\Users\Controllers\Users::extend(function($controller) {
// we only extend if its not already extended with ImportExport Behavior
if(!$controller->isClassExtendedWith('Backend.Behaviors.ImportExportController')) {
$controller->implement[] = 'Backend.Behaviors.ImportExportController';
// make sure you replace this path to your plugin directory
$extensionPath = '$/hardiksatasiya/stackdemo/user_extension_files/';
$controller->addDynamicProperty(
'importExportConfig',
$extensionPath . 'config_import_export.yaml'
);
$newListConfig = $this->makeConfig(
'$/rainlab/user/controllers/users/config_list.yaml'
);
$newListConfig->toolbar['buttons'] =
$extensionPath . '_new_list_toolbar.htm';
$controller->listConfig = $newListConfig;
}
});
}
....
2. Creating folder and files
Create folder inside your plugin's root directory and name it user_extension_files
Inside that directory
Add config_import_export.yaml with content
export:
useList: true
Add _new_list_toolbar.htm with content [ It will be just copy of plugins/rainlab/user/controllers/users/_list_toolbar.htm with slight modification]
With adding Our Brand New Shiny Export button not pasting whole code it will be too long so just pasting fragment of it.
<div data-control="toolbar">
... copied code ...
<!-- our export button -->
<a
href="<?= Backend::url('rainlab/user/users/export') ?>"
class="btn btn-primary oc-icon-sign-out">
Export
</a>
</div>
Now, when you click on export button it should export records and It will also
respect all the applied filters.
#NOTE: we are copying code to _new_list_toolbar.htm, So in future if user plugin is getting updated and they decide to add new buttons in tool-bar then we are not able to have that changes. So in that time we just need to copy & paste code from plugins/rainlab/user/controllers/users/_list_toolbar.htm to our file _new_list_toolbar.htm again. We are back in business again :) .
if any doubts please comment.
So I actually solved something like this in a different way than in the controller. I make make a decoupled front end admin system. So website owners never have to log into the backend. Which trust me has saved numerous headaches and mistakes by users that mess with things they shouldn't. So here is how I have solved this:
Install plugin Content Type by Sozonov Alexey
Make a CMS Page and make sure it has .csv at the end of the url. You
should see that the content type plugin added a content type tab in
the page settings. You can leave the html selection alone but add
text/csv as your own to the right of it.
Here is an example on how the template of the page looks like.
{% spaceless %}
NAME,EMAIL
{% for row in csv %}
{{ row.name }},{{ row.email }}
{% endfor %}
{% endspaceless %}
Here is how the CMS Page PHP Code section would look. This can allow you to do queries on the list and filter as desired. You could then check to see if the client is logged into the backend or is logged in as a user and maybe is an admin or moderator. Of course you can make a plugin and component then attach it to this page.
use Rainlab\User\Models\User;
function onStart() {
$users = User::all();
$this['csv'] = $users;
}
Side note I have used this same technique to create dynamic css, javascript, or rss feeds. I make the site map using this as well.

Custom page variable in OctoberCMS

Is it possible to use custom page variable in Configuration section in OctoberCMS?
When I do this:
url = "/blog"
layout = "default"
custom_var = "value"
==
{{ custom_var }}
my custom_var is deleting when edit page from admin panel.
It seems this was the old way of defining all stuff in single view (visual representation)
now its divided in 3 portion
1st name and url(slug) then 2nd markup(html) section
3rd code section
so you can follow new way of declaring things and it should work
use code section add this
public function onStart() {
$this['custom_var'] = 'some value';
}
use markup section and add this
<h1>{{ custom_var }}</h1>
it will work, still any issue please comment.
Yes, its possible to set custom variable into page. you can try the following code into the php section
function onEnd() {
$this->page->custom_var = 'some data';
}
And then you can use this custom_var into your layout file like
{{ this.page.custom_var }}
If you want to define new variables inside markup you can use following syntax:
{% set custom_var = 'some data' %}

Passing a field name from CFS wordpress plugin through to twig

so I'm very new to PHP and I'm having a little trouble passing a field name through.
I am using custom fields (cfs) and writing in twig for php.
Currently, I just have in my page.twig. The idea being, if an image has been set in Wordpress, the image will display in location set in page.twig.
The field name is header_image
{%if header_image %}
{{header_image}}
{% endif %}
When using Timber you can easily pass variables from your PHP controller through to your twig view. There are 2 ways to do this:
1) In functions.php you can pass variables globally to the context. The Timber Starter theme has this in it for reference. Your add_to_content function, should look like:
function add_to_context( $context ) {
global $cfs;
$context['header_image'] = $cfs->get('header_image');
Think of $context as a giant variable that passes through your variables into that actual view.
Remember: this makes your variables globally accessible, so you will need to name your CFS id's uniquely. For example global_phone_number.
2) To better structure your code, you should pass through your variables to the context, via the controller for that page. This helps ensure that you don't pass around variables that aren't being used.
In your page.twig file, you will need to add:
$context['header_image'] = $cfs->get('header_image');
Remember: the $context name is what you use in your twig file, and $cfs name is what you called it in Custom Field Suite.
Lastly:
The {% if header_image %} is good, as this will check whether a variable has been set, and then decided whether to show this block.
However, in CFS, when an image is uploaded, it gives you the option to select File URL or Attachment ID. This means you will need to handle this result in your twig file.
For example, by choosing File URL, you will need to output the {{header_image}} as:
<img src="{{header_image}}" />
You may also then want to add in the alt attribute based on the images alt text set in Wordpress media library.

how to check which controller is calling pthml page in php

I have a page in UI that has some data set in it(a textbox) .
Now i need to check where this data is set from ?
My steps were :
1. Right click on elemnt in the browser.
2. Select "inspect element" .
3. From there, I managed to find the PHTML page that is actaully displaying the data I am searching for.
In the PHTML page, "$this->resources" does the job of holding data.
Now the question is , how can i find where this data is being SET ?
Obviously a controller will do that. So i had searched for "$this->view->resources" in my project to find the respective controller, but it didn't fetched me the exact result.
Any other tips other than what I am trying to do .
P.S I am using PHP ZEND framework.
Thanks for reading .
Regards,
Sagar
I think, you want get Controller and Action names?
Using:
Zend_Controller_Front::getInstance()->getRequest()->getControllerName();
Zend_Controller_Front::getInstance()->getRequest()->getActionName();
In the same folder where 'view' folder with PHTML temlates is located, should be 'src' folder.
In it should be folder with the same name as your module.
In this folder should be the controller, with particular method (action) corresponding to your PHTML template. CamelCase in the action name maps to hyphenation in the template file name (CamelCaseAction => camel-case.phtml).

How to use PHP in the header of a drupal view?

I have a drupal view, which displays a content type filtered by a url argmuent (e.g. category). Now i'd like to add a link in top of my view, which allows my users to add a new node. In the add form the argument field should be prepopulated with the value from the filter.
I think to archive this, i have to install the prepopulate module and add some php-code to the header section of my view, which generates the link.
Any suggestions how to manage this?
If you are wanting to add php code to get the arguments passed to views in the views header section, do the following:
Make sure that the PHP filter is turned on; this is a module that can be enabled
In the header section use the following code:
$view = views_get_current_view();
// [0] is first arg, [1] is second etc.
$argumentOutput = $view-> args[0];
Remember to have the input format set to PHP code. This will get you the argument passed to views.
It would be a lot easier to override the template for the view and insert the code/markup there. Check out the Theming infomation inside the view to find out what to call your template.

Categories