cakephp placeholder if image not exist - cakephp

I have a database with images associated to each entry.
Anyhow, some entries don't have an image.
Googling I found some js and php way to apply in the view, but I'm pretty sure there is a (much nicer) MVC-way to do this, just I don't know how..
Is there a nice Cake-way to display placeholder image where none exist?

Supposed the data you set from the controller named 'post'. And you have 'your_default_image' in your /app/webroot/img/
<?php
if(empty($post['Post']['img'])) {
echo $this->Html->image($post['Post']['img']);
} else {
echo $this->Html->image('your_default_image');
}
And you can wrap those logic in your helper.

You can use placehold.it anywhere you need for placeholding images.
Just use http://placehold.it/WxH like http://placehold.it/250x250 and you're set.

Related

AFC Repeater and wp-rest api in React.js

Need to render ACF repeater in react. I am able to display ACF text Fields but not repeater fields. Need to find out if anyone has an example of how to map through a repeater field.
Repeater field group Is called Skills.
Im also new in this stuff, but I will try to help you.
So, the first thing that you need is to download and install ACF to REST API plugin so you can use ACF with Wordpress API. I assume, that you already have it, because as you said before - you can display text fields.
Once you can send data through Wordpress API, you need to preview of JSON sent by Wordpress (in this case), so you can display necessary data. Mine is called React Developer Tools and I installed it as Chrome extension.
Link to Chrome store
It should look like this:
As you can see, my component is called Home.js, yours may be called differently. Chose component that is fetching all the data that you need.
Now, you just need to use your repeater. It would be much easier if you showed us your code. I don't really know what kind of data you are calling through api, so I guess these are pages.
{ pages[0].acf.technologie_lista.map ( (field, index) => (
<div key={index} className="single-field">
{ field.nazwa_technologii }
</div>
) ) }
Let's break it down.
1 - My project contains two pages. I have chosen the first one, because only this one has needed ACF fields. technologie_lista is acf field name.
2 - You need to use map function to list all posts. You need to assign key to each element.
nazwa_technologii is just a repeater sub field name.
And that's all. I might make some rookie mistakes, but it work's for me. I hope that i helped. Cheers!

Drupal 7. How do I change the CSS of the node when I click "Read More?"

I'm creating a custom theme (as a n00b). Basically, I display content on the main page as small div blocks.
my node.tpl.php looks something like this:
<div class="content-block">
default node.tpl.php stuff here
</div>
This works OK on the front page. However, when I click "Read More," to take me to the full article page, /node/[nodeId], obviously, it still shows as the little CSS content-block div.
I'd like to show the full article using a different CSS class. What's the best way to do this?
Yes, there are some best ways to accomplish that.
First of all I am not sure, whether you are using separate front page tpl or not. If not then please try to use page--front.tpl.php. Where you can provide your desired format to create the proper front page. Why this is needed, cause you won't need to modify the node.tpl.php file. So inside this front tpl file you can put your desired code.
<div class="content-block">
default node.tpl.php stuff here
</div>
Also you can use the Panel module to create a front page. So you won't be needed to modify the nope tpl file. Always try to avoid modifying tpl file. Panel module makes it easier.
Secondly, you should not touch the node.tpl.php file. Once you will modify the file, you won't be able to re-define as default. So keep that as it is and revert back it to the default file. Now if you send some article link, it won't break.
So this is it. If this helps you, then please like :)
Keep Drupalizing :)

Drupal - How to add data-attributes to <li> items of a <ul> Menu

This is such a great script called fullPage.js located here: https://github.com/alvarotrigo/fullPage.js
I managed to load the js file and css file and even with the Void Menu Module, managed to call anchor links, but I'm struggling with the data attributes part of the implementation of this cool code! Any help is greatly appreciated! How do I get data-attributes to work in Drupal 7.25? I mean, I need to add them to the menu, so that the html looks something like this:
<ul id="menu">
<li data-menuanchor="firstPage">First slide</li>
<li data-menuanchor="secondPage">Second slide</li>
<li data-menuanchor="3rdPage">Third slide</li>
<li data-menuanchor="4thpage">Fourth slide</li>
</ul>
Just want to add it to the Main Menu in Drupal. Any idea how? Am trying to implement this on my homepage and using the Adaptive Sub-Theme with Display Suite Module installed, if that helps any.
Have added the following to template.php, but it makes no difference at all:
function MY_THEME_NAME_menu_link(&$variables) {
$variables['element']['#attributes']['data-menuanchor'] = substr($variables['element']['href'], 1);
return theme_menu_link($variables);
}
What am I doing wrong here?
EDIT
You can download the sitemaniacs theme here: http://dream-portal.net/dpdowns/MyProblem.zip
You can just download Adaptive Theme from Drupal here and than copy sitemaniacs to your sites/all/themes folder and than just enable the theme and go to your homepage.
There is also a folder called fullPage that is the example of it working perfectly fine without Drupal.
Files to take note of:
/sites/all/themes/sitemaniacs/scripts/custom.js
/sites/all/themes/sitemaniacs/template.php
/sites/all/themes/sitemaniacs/css/example2.css
/sites/all/themes/sitemaniacs/sitemaniacs.info
Let me know if you need anything else, k?
And Thank YOU!!! What I've done is create 4 Basic Pages, and using the Field Formatter Class Module to give the body field a class (when managing the Display) defined as section, than promoted to front page. Than I used HTML within another Node Type and used NodeBlock Module to output it as a menu item within the Menu Bar section of the blocks, with url <front>. Let me know if you need anything else for this, but I can't get it to work using this simple approach. I've tried tons of ways of doing it also, but no go for me. If you find a method that works, or perhaps something is wrong somewhere else, please please let me know.... I'm pulling out my hair on this one.
Have tried straight HTML, and now getting this:
Uncaught TypeError: Cannot read property 'top' of undefined jquery.fullPage.js?mzf0rp:506
scrollPage jquery.fullPage.js?mzf0rp:506
doneResizing jquery.fullPage.js?mzf0rp:889
The first error seems to occur in the scrollPage function on this line here:
var dtop = dest !== null ? dest.top : null;
Looks like dest.top is not defined on .top. Any idea why?
Use the Menu Link Attributes module that will let you add custom attributes per menu item.
for "href" you can use <'front'>#4thpage(front without any “cotation”) in path in menu that you create in Structure > Menus and use the Menu Link Attributes module for other attributes

How to generate 'a href="javascript:void(0)"' like link in cakephp?

How to generate 'a href="javascript:void(0)"' like link in CakePHP?
I make an application, the content will insert into the editor textarea when user click a list of image. I add a class to these images and write some code in the javascript file. Everything is going well.
But the link of the image is a URL address, but not 'href="javascript:void(0)' like URL. Anyone could tell me how to make it in CakePHP?
Thanks in advance!
<?php
echo $this->Html->link(
'/path/to/image/',
'javascript:void(0)'
);
?>
You can either set a path to the image or use the Html helper to generate the image tag code. The second parameter will set the href.
Don't believe there is any dynamic way, however when you are creating your form element you can set it in the options array 'href' => 'javascript:void(0)'

CakePHP Elements - how to put javascript?

I've just started using cakephp elements and they're awesome (I used to use include).
I have an element gallery and an element called comments (for a certain page) and for them I have some javascript code attached to both of them. Do you have any suggestions on how I could include that javascript code in the element? If I simply add it like that it will load javascript before loading the html after the element and I don't think it's very wise.
You could put the Javascript code directly into the element file, or put the Javascript code in into your webroot folder, <cake directory>/app/webroot/js/ and include the file in your layout by using the HTML helper:
echo $html->script("myCode");
If you're worried about the Javascript code executing before the page has completely loaded, then use window.onload or $(document).ready() if you're using JQuery.
If I understand you correctly, you want to have JS specific to a page loading in the header when you call a certain element, but that the JS could be different for each element. You also want the JS to be referenced at the beginning of your HTML document.
This is actually quite easy to do. Just make sure that you have <?php echo $scripts_for_layout; ?> in you <head> tag in the layout you are using.
Then, within the element, just do:
<?php $this->Html->script("js_file", array("inline"=>false)); ?>
js_file is the name of your JavaScript file in app/webroot/js/. In this case, the file would be called js_file.js but you must leave off the .js when referencing it as above.
It doesn't matter where abouts in the element file you put this because the "inline"=>false part ensures it won't actually appear at that stage in the code. Instead, it will appear in the <head> wherever you put <?php echo $scripts_for_layout; ?> in your layout.
In cakephp 3 instead of array('inline' => false) you should use array('block' => true) if anyone is looking for that answer like I was.

Resources