Update: I am creating long running background task in Symfony framework using Process functionality. As part of that:
- User will complete the form and click submit button
- This will start the background process using below script
$rootDir = $this->get('kernel')->getRootDir();
$adk_process = new Process('php ../bin/console app:adkaction ' . $numcampaigns . ', ' . $timezone . ',' . $depdate);
$adk_process->setWorkingDirectory($rootDir);
$adk_process->setTimeout(null);
$adk_process->start();
In order for user to be able to go do other things, we close current session and want to render specific twig tempalate and pass specific variable to it using code below
$ignore_user_abort(true);
$strURL = $rootDir;
header("Location: " . $strURL, false);
header("Connection: close", true);
header("Content-Encoding: none");
header("Content-Length: 0", true);
flush();
ob_flush();
session_write_close();
This works fine however I cannot seem to find the way how to pass a variable this way. My Twig Template is like this
{% if currprogress is defined %}
<div class="col-md-4 col-sm-4 col-xs-12">
<div class="x_panel">
<div class="x_title">
<div class="progress">
<div class="progress-bar progress-bar-striped active"
role="progressbar" aria-valuenow={{currprogress}}
aria-valuemin="0" aria-valuemax="100" style="width:{{currprogress}}%">
Campaign Generation - {{currprogress}}%
</div>
</div>
</div>
</div>
</div>
{% endif %}
So once currprogress variable value is passed to Twig, it would slightly change. Any idea on how to do it using header + link.
Related
I am building a theme and I have images displaying on the site via ACF. I am trying to get the alt text that the client enters when they upload the image. I understand that the image return format should be set to image array or ID, but not URL. Mine were set to URL quite some time ago and I am having trouble revising my code to reflect the alt text when inspected. Here is my current function that displays staff repeater.
function meetOurTeamListing() {
if(have_rows('meet_our_team')):
while(have_rows('meet_our_team')): the_row();
$staffImage = get_sub_field('staff_image');
$staffName = get_sub_field('staff_name');
$staffTitle = get_sub_field('staff_title');
$staffBio = get_sub_field('staff_bio');
$html =
'
<div class="myBtn staff-item">
<img src="'.$staffImage.'" />
<p class="staff-name">'.$staffName.'</p>
<p class="staff-title">'.$staffTitle.'</p>
</div>
<!-- The Modal -->
<div class="myModal modal">
<!-- Modal content -->
<div class="modal-content">
<div class="modal-header">
<span class="close">×</span>
<div class="modal-header-text">
<p class="modal-name">'.$staffName.'</p>
<p class="modal-pipe">|</p>
<p class="modal-title">'.$staffTitle.'</p>
</div>
</div>
<img src="'.$staffImage.'" />
<div class="modal-body">'.$staffBio.'</div>
</div>
</div>
';
echo $html;
endwhile;
endif;
}
Obviously the sub-field would need to be converted to an image array, however simply switching the radio selector from "image url" to "image array" breaks the images on the front end. Given this context, how can I implement alt text from the WP database dynamically?
Problem solved. Thanks Nicole!
My problem was that when set up the custom fields that took images, I set them to Image URL. This is good if you just want to quickly display an image because it just returns a string. It should be set to Image Array. This results in an itemized list of key/value pairs (ie. alt text) that are accessible in the theme template.
To display your image that is set to an Image Array, here's the syntax:
<img src="'.esc_url($image['url']).'" alt="'.esc_attr($image['alt']).'" />
This works only inside of my PHP function. Alternatively, the following can be used inside a template file among the HTML:
<img src="<?php echo esc_url($image['url']); ?>" alt="<?php echo esc_attr($image['alt']); ?>" />
Hello good people of StackOverflow, I was wondering is there any way I can use advanced custom fields in shortcode?
function highlight($atts) {
return '
<div class="col-lg-6 ">
<div class="highlighted">
<p class="page-title">TEST</p>
</div>
</div>';
}
add_shortcode('scbox', 'highlight');
so I would like to put something like {{ the_field('text') }} where the "TEST" is now, I'm using blade template if its of any help
You can use the get_field() function to obtain the value that you are looking for. (get_field() will return the value, whereas the_field() will print the value wherever invoked.)
You can then concatenate the value into your returned string:
function highlight($atts) {
$text = get_field('text');
return '
<div class="col-lg-6 ">
<div class="highlighted">
<p class="page-title">' . $text . '</p>
</div>
</div>';
}
I have a login form and i am setting the flash message on invalid login like this in the controller:
$this->Session->setFlash(__('Invalid username or password'));
And displaying the message like below in ctp file :
<div id="error" >
<?php
echo $this->Session->flash();
?>
</div>
CSS for the div is as follows:
#error {
padding:10px;
display:inline-block;
margin-right: 18px;
font-size:16px;
font-weight:600;
position:relative;
top:20px;
}
When the page is loaded an empty flash message is shown.
How to check if $this->Session->flash(); is empty and display div only if its not empty??
When the message is empty flash message on page load it looks like this:
Similar way it looks like this when message is set:
Try this:
Create your Custom Elements with Error Alert, Success Alert, etc...
/View/Elements/error_alert.ctp
<!-- Example Bootstrap Error Alert -->
<div class="alert alert-danger">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong><?php echo $message ?></strong>
</div>
/View/Elements/success_alert.ctp
<!-- Example Bootstrap Succes Alert -->
<div class="alert alert-success">
<button type="button" class="close" data-dismiss="alert">×</button>
<strong><?php echo $message ?></strong>
</div>
In your Views add this, It´s appear only when Message must be displayed
<!-- It´s will work for all alert types (not only for error alerts) -->
<div class="col-md-12">
<?php echo $this->Session->flash(); ?>
</div>
if you want use one add this in your controllers:
For success Alert
$this->Session->setFlash('Success message!', 'success_alert');
For error Alert
$this->Session->setFlash('Error message!', 'error_alert');
New user of D8 : my problem is to access to fields in a view or even in general with Drupal 8.
As we could do with ACF in Wordpress.
a {{ kint() }} crash my chrome but works with Firefox to explore the content var.
Unfortunately I do not managed to find and use fields' variables in my view.
I create a new view, which actually display the last three articles. These are well displayed in ugly list but I want to extract fields to put them in a custom html integration.
I create and use a new template for the view :
x node--view--liste-des-actualites--page-2.html.twig
In a custom parent :
x node--page-accueil.html.twig
But when I try to kint() content in my node--view--liste-des-actualites--page-2.html.twig, I have the custom field of the page (Page accueil) and can't find the article's one.
I managed to do it in my custom page but not in this view.
{%
set classes = [
'node',
'node--type-' ~ node.bundle|clean_class,
node.isPromoted() ? 'node--promoted',
node.isSticky() ? 'node--sticky',
not node.isPublished() ? 'node--unpublished',
view_mode ? 'node--view-mode-' ~ view_mode|clean_class,
'clearfix',
]
%}
{{ attach_library('classy/node') }}
<article{{ attributes.addClass(classes) }}>
<div{{ content_attributes.addClass('node__content', 'clearfix') }}>
{{ content }}
<div class="col-lg-4 col-md-4 col-sm-4 col-xs-4">
<a href="{{ LINK_VAR }}" class="bloc-type">
<div class="categ categ_projet">{{ CATEGORY_VAR }}</div>
<div class="img"> <img src="{{ IMAGESRC_VAR }}" alt=""> </div>
<span class="wrapper">
<p class="date">{{ DATE_VAR }}</p>
<h3>{{ TITLE_VAR }}</h3>
</span>
</a>
</div>
</div>
</article>
EDIT
I managed to guess some fields but this is definitely not a good way to find variables..
{{ node.label }} + {{ content.field_tags }} (But I do not want a rendered one, I just want the text/value)
if you use kint(); to debug large arrays can crash your browser.
I would suggest to use the devel module https://www.drupal.org/project/devel. With devel you can debug your arrays inside of the Drupal8 UI for each content type, block or view.
In my case i use the UI of devel (additional tab on each content). in the module settings, you can chose how devel debugs, the error handling and the output.
As the OP commented it is possible to use a preprocess to display the array on your site:
function <themename>_preprocess_page(&$variables) {
dpm($variables);
}
I wanted to create specially styled columns, but make it easy for the client to still edit the content of the column.. so I created my own shortcodes to setup the row and columns inside. (This is a custom wordpress template I created for a specific client).
Here is the code in my function.php for the two shortcodes 'member-row' & 'member':
add_shortcode('member-row', function ($content = null) {
return '<div class="row">
<br />'.do_shortcode($content).'</div>';
});
add_shortcode('member', function ($atts, $content = null) {
extract(shortcode_atts(array(
'color' => 'white',
), $atts));
return '<div class="col-md-6 col-sm-6 col-xs-12 md-margin-bottom-40">
<div class="member-col funny-boxes funny-boxes-top-'.$color.'">'.do_shortcode($content).'</div>
</div>';
});
I've also added the .do_shortcode($content). on the inner-nested 'member' column shortcode - just in case the client wants to add other shortcodes inside of each column. (Note: The problem still occurs even if I change the 'member' shortcode to just use .$content.)
Here is the code I entered in the text side of the wordpress page editor
[member-row]
[member color="gold"]
<img class="aligncenter img-responsive" src="http://localhost/test/testWP/wp-content/uploads/2015/07/logo1.jpg" alt="logo" />
<strong>Company 1</strong>
Address
Telephone
www.example1.com
[/member][member color="yellow"]
<img class="aligncenter img-responsive" src="http://localhost/test/testWP/wp-content/uploads/2015/07/logo2.jpg" alt="logo" />
<strong>Company 2</strong>
Address
Telephone
www.example2.com
[/member]
[/member-row]
When I save, and click on 'view page', the section where this has been added doesn't display anything.
This is the html code that is produced:
<div class="row">
<br /></div>
So it is only executing the 'member-row' shortcode, and not the nested 'member' shortcode columns.
This is the correct html code I was expecting:
<div class="row">
<br />
<div class="col-md-6 col-sm-6 col-xs-12 md-margin-bottom-40">
<div class="member-col funny-boxes funny-boxes-top-gold">
<img class="aligncenter img-responsive" src="http://localhost/test/testWP/wp-content/uploads/2015/07/logo1.jpg" alt="logo" />
<strong>Company 1</strong>
Address
Telephone
www.example1.com
</div>
</div>
<div class="col-md-6 col-sm-6 col-xs-12 md-margin-bottom-40">
<div class="member-col funny-boxes funny-boxes-top-yellow">
<img class="aligncenter img-responsive" src="http://localhost/test/testWP/wp-content/uploads/2015/07/logo2.jpg" alt="logo" />
<strong>Company 2</strong>
Address
Telephone
www.example2.com
</div>
</div>
</div>
When I just remove the surrounding [member-row] and [/member-row] shortcodes from the editor window, then the 'member' column shortcode does show up properly on the webpage.. so it appears it is just being inside of the 'member-row' shortcode that causes the problem.
Note: I did try adding the add_filter('the_content', 'do_shortcode', 10); line to the bottom of my function.php file, but it didn't seem to make any difference, so I removed it.
Hopefully I have just made some typo error.. Any help will be greatly appreciated!
Thanks.
I figured out the problem.
I had to add $atts to the function for the member-row shortcode as follows:
add_shortcode('member-row', function ($atts, $content = null) {
return '<div class="row">
<br />'.do_shortcode($content).'</div>';
});
It now works as expected!!
I am not sure why I needed to add $atts, as this shortcode is not passing attributes or setting default ones, but it appears you need it if you are going to use enclosing shortcode with $content. (Maybe it is needed because $content is defined as the 'second' parameter???)
I just read in the WP Codex for shortcode api that
"Three parameters {$atts, $content, $tag} are passed to the shortcode
callback function. You can choose to use any number of them including
none of them."
So that seems to me that you could use the $content by itself... but as I just proved that $content can't be by itself - I am still unsure of the reason that my solution worked for me. (Just glad it did!)
If anyone knows the rule or reason for why the $atts is needed to make this work, I would appreciate the comment - to clarify this for myself, and anyone else that has the same problem.
Thanks!