Passing a field name from CFS wordpress plugin through to twig - php

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.

Related

How do I use variables in a different place?

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.

Dynamically grab latest post OctoberCMS Blog plugin

I asked this question in the October forums but 12 hours later the post still says "unapproved" and I don't believe it's visible. Here's a copy-paste:
What I want to do is grab the latest blog post and display it on the homepage of the website. I can do it currently with my own post partial and the posts variable injected by the blogPosts component like so:
[blogPosts]
pageNumber = "{{ :page }}"
postsPerPage = 10
noPostsMessage = "No posts found"
sortOrder = "published_at desc"
categoryPage = "blog/category"
postPage = "blog/post"
==
{% partial 'site/blogpost' post=posts|last %}
However, I'd like to do this with the default blogpost component that comes with the plugin, but the only way to pass the post to the component seems to be by using the slug in the url, which doesn't really work for the homepage. How can I achieve this?
It is possible to use blogPost component but fetching last post slug and pass to it seems not good practice
what you do is you can use [blogPosts] component and set proper setting there to get latest/last blog
to make it possible
Posts per page : 1 [ as we need only last latest post ]
Post Order : Published(desc) [ you can change as you need ]
now just use proper markup to render it use default or just override to customise it.
Note : It will return list of post but in list there will be only 1 post as demanded so for custom markup just take care of that.
if any doubt please comment.
So, as with most things when it comes to development... RTFM.
All the info I needed was in the Components section of the October CMS docs, and of course I was only looking in the plugin docs. What I ended up doing was overriding the default component partial with my own and then passing the slug to the component. My original reason for wanting to use the default partial was that my custom partial wasn't rendering images, but the default would. I just needed to steal the line <p>{{ post.content_html|raw }}</p> to get that to work.
[blogPost]
==
...
{% component 'blogPost' slug=posts|last.slug %}
Additional info: With the above solution your template pulls all blog posts in the database, which means if you have a lot of posts this could (and most likely will) affect performance. Turns out October templates have a PHP section. The proper thing to do is to remove the blogPosts component and grab the latest post Model like so:
[blogPost]
==
<?
use RainLab\Blog\Models\Post;
function onStart()
{
$this['latestPost'] = Post::latest()->first();
}
?>
==
{% component 'blogPost' slug=latestPost.slug %}
Note: The blogPost component injects a post variable which will override a post variable defined in the PHP section. This is way the variable is labeled latestPost.

WordPress custom URL and custom template

I have a custom template named "eshop". In there, it calls a separate API out from WordPress and clicking on a link returns something like : eshop/ABC-123/
Is there a way I can make all the URLs that contain the word "eshop" to use the custom template I have created?
Thanks!
Step one: Pull the URI into a variable (http://webcheatsheet.com/php/get_current_page_url.php)
Step two: Search the variable string for the string you're looking for (How do I check if a string contains a specific word in PHP?)
Step three: use an if statement (If variable includes string "eshop", include the template) with a PHP include statement.

Wordpress template tag inside of a another template tag

Here's a fun one. I'm using advanced custom fields inside of a template to pull a field called Applications with the following code.
the_field('applications');
The problem is that this won't pull the actual content without the page ID along with it, for example:
the_field('applications','42');
where the page ID is 42.
What I would like to do is use another template tag to pull the existing page ID with something like this in place of the 42, so that it will content specific to that page:
the_ID();
Which, in a perfect world would look like so:
the_field('applications','the_ID();');
This is obviously ridiculous and doesn't work, but I don't know what I need to do to get this to actually work.
Thanks!
the_ID() will automatically echo whatever is returned, whereas get_the_ID() will return that value so that it can be stored in a variable, or passed as an argument (which is the deal in this case).
<?php the_field('applications',get_the_ID()); ?>
http://codex.wordpress.org/Function_Reference/get_the_ID

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