flash message in yii2not work when use that with js code - yii2

hi i have flash message to show success message and i want hide that after 3 seconds . i use js code but in line 3 when i use view::POS_READY i get error to not found that class so i comment this line and after that js code not worked and not fade my message.
how can fix this problem?
this is my show flash code in view:
<?php if(Yii::$app->session->hasFlash('flashMessage')):?>
<div class="flash-success">
<?php echo Yii::$app->session->getFlash('flashMessage'); ?>
<?php
$this->registerJs(
"$('.flash-success').animate({opacity: 1.0}, 3000).fadeOut('slow');",
//view::POS_READY,
'myHideEffect'
);
?>
set flash in controller:
if ($model->load(Yii::$app->request->post()) && $model->save()) {
Yii::$app->session->setFlash('flashMessage', 'success');
return $this->redirect('index.php');
}

Try this... or you can "use \yii\web\View;" on top of the view file
<?php if(Yii::$app->session->hasFlash('flashMessage')):?>
<div class="flash-success">
<?php echo Yii::$app->session->getFlash('flashMessage'); ?>
<?php
$this->registerJs(
"$('.flash-success').animate({opacity: 1.0}, 3000).fadeOut('slow');",
\yii\web\View::POS_READY,
'myHideEffect'
);
?>

The accepted answer probably works great, but I would like to propose using the Growl widget. Fading is just a part of the features.

Related

This statement returning Yii::$app->request->baseUrl empty

i am new you yii2 and i want to get baseUrl in yii2 for that i had written following statement but it return nothing
<?php echo Yii::$app->request->baseUrl; ?>
Best way to get Base url in Yii2 is:
Yii::$app->HomeUrl
Hope this will help you.
try with : <?php echo Yii::$app->getUrlManager()->baseUrl ?>
Hope this will work.

yii2 include html code inside Html::button

I need to include some html code inside Html::button in yii2. According to the Class yii\helpers\BaseHtml i can pass html code to $content. Please, correct me if i am wrong. So i wrote:
<?= Html::button{'<div class=\'row\'></div>,[]); ?>
and it works. But what if the code i want to insert is big? How do i include big code with less pain?
Thank you very much.
You could use views and renderPartial
e.g. in your view
<?php
$html = $this->context->renderPartial('sub_view', [
'attribute' => 'test',
]);
echo Html::button($html,[]);
?>
and in your subview your static html or dynamic code
<h1>static html code<h1>
<div>
<?php
echo $attribute
?>
</div>

switching between regular html and jQuery mobile site acting strange [duplicate]

I have a wordpress site, that need to show pages using swipe, I choose to use Jquery Mobile, and I get it working fine. Now, we have 2 languages on site, using wpml plugin. And my Swipe Code works well, except when we use Change language button swipe fails.
Details on issue.
We have only 3 Text Only page in our website, in 2 language. And in Footer we have link to change language. Also client hate to have Ajax page loading, so what I did is I create three Div with data-role=page and put data-next, data-prev as #div-$postid. So the navigation works absolute fine. I put footer outside from data-role=page.
Now, when I click change button in footer, it load the english page [I saw it using Fiddler] and then take first data-role=page from server and replace /slide its content. However since it only pick the first data role, all other english page doesn't get in HTML [it just update DOM and doesn't navigate to english version]. so swipe fails as other english pages are not in dom.
Also, footer is not changing, so what I want is: can we simple force a Link to navigate instead of going swipe way? Jquery Mobile is enforcing swipe on all A tags, I do not want swipe to works anything outside data-role=page.
Hope I make sense.
Edit here is code: [not sure if this code will help at all]
<?php
get_header();
global $post;
$args = array('post_type' => 'mobile_slide','showposts' => '-1', "order" => "DESC");
$the_query = new WP_Query($args);
if($the_query->have_posts()){
while($the_query->have_posts()) { $the_query->the_post();
$prev =get_previous_post();
$next =get_next_post();
if($prev) {
$prev = "#page-" . $prev->ID; //get_permalink($prev->ID);
} else {
$prev='';
}
if($next) {
$next = "#page-".$next->ID; //get_permalink($next->ID);
} else {
$next='';
}
if (has_post_thumbnail( $post->ID ) ) {
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'slider_image' ); ?>
<div id="page-<?php echo $post->ID; ?>" data-dom-cache="true" data-transition="slide" class="page" data-role="page" data-prev="<?php echo $prev; ?>" data-next="<?php echo $next; ?>" style="background-image:url('<?php echo $image[0]; ?>'); background-position:center center; background-color:#000; background-repeat:no-repeat; ">
<?php } else { ?>
<div id="page-<?php echo $post->ID; ?>" data-dom-cache="true" data-transition="slide" class="page" data-role="page" data-prev="<?php echo $prev; ?>" data-next="<?php echo $next; ?>">
<?php } ?>
<div class="post_box">
<h2><blockquote><?php the_title(); ?></blockquote></h2>
<div class="post_entry">
<?php the_content(); ?>
</div>
</div><!-- post_box -->
</div>
<?php }
} ?>
<?php get_footer(); ?>
This is all I have, except that get_footer use Ul li based list where on LI change based on language variable, to show different images for either language.
To stop Ajax from loading pages/links, add to link anchor data-rel="external" or data-ajax="false". This will load page normally without any transition.
Reference: jQuery Mobile - Links
For those who have similar problem, I fix it by using following:
1) I add a "noswipe" class to A Tag so I can refer it in Jquery
2) I add following code
$(function(){
$(".noswipe").click(function(){
window.location.href= $(this).attr("href");
return false;
});
});
The above code simply enforce to skip the Mobile's parsing and calling and works for my case.

Codeigniter form elements as table

I'm trying to build a form in Codeigniter so that its elements are part of a table and are therefore aligned nicely. Here's the view page:
<div id="login">
<h3>Log in</h3>
<?php
$attributes = array('id'=>'form_login');
echo validation_errors();
echo form_open('login/main', $attributes);
//probably a bad idea to load libraries in views, but what the heck?!
$this->load->library('table');
$this->table->add_row('Username', form_input('username'));
echo $this->table->generate();
//echo 'Username: ';
//echo form_input('username') . '</br>';
echo 'Password: ';
echo form_password('password') . '</br>';
echo form_submit('submit', 'Log in');
?>
<br/><br/>
Forgot Password <br/>
New User? Register
</div>
The two lines commented out is how it was earlier. Now I'm getting the following error: Call to a member function add_row() on a non-object. Why is table a non-object? I've tried loading the library in the controller but the error persists. Please help!
In codeigniter, the global codeigniter object is not available in your views, so you have to get a reference to it.
Try the following
$ci =& get_instance();
Put that at the top, and replace your calls to $this like so:
$ci->load->library('table');
$ci->table->add_row('Username', form_input('username'));
echo $ci->table->generate();

EntityMalformedException when retrieving Drupal 7 custom field

I am playing with Drupal and I am trying to add a second line to the site slogan.
The following is the piece of page.tpl.php where I am working.
<?php if ($site_name || $site_slogan): ?>
<div id="name-and-slogan" class="hgroup">
<?php if ($site_name): ?>
<h1 class="site-name">
<a href="<?php print $front_page; ?>" title="<?php print t('Home'); ?>">
<?php print $site_name; ?>
</a>
</h1>
<?php endif; ?>
<?php if ($site_slogan): ?>
<p class="site-slogan"><?php print $site_slogan; ?></p>
<?php endif; ?>
<?php
/* ADDED */
$node = menu_get_object();
$siteslogan2 = field_get_items('node', $node, 'field_siteslogan2');
?>
<?php if ($siteslogan2): ?>
<p class="site-slogan2"><?php print $siteslogan2; ?></p>
<?php endif; ?>
</div>
<?php endif; ?>
I basically added a new Content Type with a field called siteslogan2 (field_siteslogan2) and now I would like to retrieve and show it here.
The first problem is that the $node var is not defined (even if according to the documentation it should be). The second problem is that I receive EntityMalformedException: Missing bundle property on entity of type node. in entity_extract_ids() when I define it manually and run it.
First of all, is this the right approach to the problem? Secondly, why do I receive the EntityMalformedException and how can I fix it?
var_dump($node) produces NULL. It must be the way I get the $node content that is not good. The doc is a bit cryptic to me when it says:
$node: The node object, if there is an automatically-loaded node associated with the page, and the node ID is the second argument in the page's path (e.g. node/12345 and node/12345/revisions, but not comment/reply/12345).
This sort of thing is can be done using a preprocessing function in your theme's template.php to define and set a variable in the $varables array.
This variable then becomes available to whichever template ( page, node, form, etc ) you have done the preprocessing on. Your theme's template.php file will most likely have comments on how to do this.
for instance, doing this in the template.php creates or modifies the value of the variable $display_header, making it available for use in node.tpl.php
function yourthemename_preprocess_node(&$variables, $hook) {
$variables['display_header'] = false;
}
You can then modify the node template file to use this variable.
For something simple like a sub-slogan, you can add a setting to the theme, so it shows up on the theme's configuration page like any other theme setting.
This requires implementing this function in your theme's theme-settings.php:
function yourthemename_form_system_theme_settings_alter(&$form, $form_state) {}
This provides information to the settings form regarding the new setting. like this:
https://api.drupal.org/api/drupal/modules!system!theme.api.php/function/hook_form_system_theme_settings_alter/7
Hopefully that's enough to get you started.