I have a custom block which has a default view with a form in it. When that form is submitted I set a controller flag and the block is (should be) updated to display more information.
The problem is my view is treating it like I have no data/variables set
Controller.php
public $unlocked = false;
public $employer;
public $shortname = "not loaded";
public function on_page_view() { //already overridden because I'm compiling LESS
...
$this->setViewVariables();
}
function setViewVariables() {
$this->set('shortname', $this->shortname);
$this->set('is_unlocked', $this->unlocked);
...
}
public function action_accesscode_unlock() {
$this->unlocked = true;
$this->shortname = "fred";
//Have also tried calling $this->setViewVariables(); as well,
//before I realised view() and on_page_view() were called after this anyway
}
View.php
<?php if ( !$is_unlocked ) {
echo $shortname; //does correctly display the default value
?>
<form action="<?php echo $this->action('accesscode_unlock')?>" id="accessform" method="post">
...
</form>
<?php } else {
//THIS section is never displayed (always reloads form with default name)
echo $shortname;
} ?>
What am I doing wrong here so that the new variable values are never set in the view?
Edit
After replying to JohnTheFish I just realised, the LESS compilation code I use includes the following lines (used to get block path). Could this be changing the instance used for different parts of the lifecycle?
$bv = new BlockView();
$bv->setController($this);
$bv->setBlockObject($this->getBlockObject());
on_page_view runs before action_accesscode_unlock, so the logic of action_accesscode_unlock does not happen until after the variables are set.
You could try adding a call to setViewVariables to the end of action_accesscode_unlock.
(In answer to your edit, yes, it could)
Related
I read carefully How to display notice in admin panel on Plugin Activation? which is similar, but I could not make it work correctly
class Shoutbox {
function display_notice() {
echo '<div class="updated">
<p>I am a little yellow notice.</p>
</div>';
}
public static function pluginActivated() {
//exit("Plugin has been actived"); // this is displayed when not commented
add_action('admin_notices','display_notice'); // this notice is never displayed.
}
}
//add_action('admin_notices', array('Shoutbox', 'display_notice')); // this is displayed when not commented
new Shoutbox();
I also tried with
public static function display_notice() {
Any idea on how to display the admin notice inside pluginActivated() ?
You need to pass the method to the add_action function as an array, with the first element being the class name, and the second element being the method name. you can follow my code :
class Shoutbox {
public static function display_notice() {
echo '<div class="updated">
<p>I am a little yellow notice.</p>
</div>';
}
public static function pluginActivated() {
add_action('admin_notices', array(__CLASS__, 'display_notice'));
}
}
new Shoutbox();
I trying to create a custom settings page for my plugin, but I'm unable to make the page display a custom settings page, to make things simple I just want to display the section header.
Here is what I have.
private function add_hooks() {
add_action('admin_menu', array($this,'register_menu'));
add_action("admin_init", array($this,"display_options"));
}
public function register_menu() {
add_menu_page('Feed','API FEED','manage_options','adt-feed',array($this,'include_admin_page'),'dashicons-format-image');
}
public function include_admin_page() {
return include_once( PLUGIN_PATH 'admin/view/config-page.php' );
}
public function display_options()
{
//section name, display name, callback to print description of section, page to which section is attached.
add_settings_section("adt_general_section", "Header Options", array($this, "display_header_options_content"), "adt-feed");
}
public function display_header_options_content() { //THIS NEVER GETS CALLED
echo "PLUGIN HEADER SECTION";
}
and them in my config-page.php i have:
<div class="wrap">
<div id="icon-options-general" class="icon32"></div>
<h1>ADT Feed Options</h1>
<form method="post" action="options.php">
<?php
//add_settings_section callback is displayed here. For every new section we need to call settings_fields.
settings_fields("adt_general_section");
// Add the submit button to serialize the options
submit_button();
?>
</form>
</div>
Using some "die()" calls i manage to find that display_header_options_content never gets called, if i change "adt-feed" for "general" in the add_settings_section, i can see the message: "PLUGIN HEADER SECTION" in the General settings page.
thanks in advance for any help!
You need to call
do_settings_sections( 'adt-feed' );
in config-page.php.
A good place to put this is just before the call to submit_button();
I am having a hard time figuring out how the exactly the routing works in JomSocial. Does anyone know how to create a new view?
First of all, you create a controller to make a request of a view:
file: controllers/hello.php
<?php
// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die();
class CommunityHelloController extends CommunityBaseController
{
function helloWorld() //index.php?option=com_community&view=hello&task=helloWorld
{
$document = JFactory::getDocument();
$viewType = $document->getType();
$view = $this->getView('hello', '' , $viewType);
echo $view->get('helloWorld');
}
function hello() //index.php?option=com_community&view=hello&task=hello
{
$document = JFactory::getDocument();
$viewType = $document->getType();
$view = $this->getView('hello', '' , $viewType);
echo $view->get('helloWorld');
}
}
?>
View: views/hello/view.html.php
Here you place variables that will be passed to template file
For example:
<?php
defined('_JEXEC') or die('Restricted access');
jimport ( 'joomla.application.component.view' );
class CommunityViewHello extends CommunityView {
function helloWorld() //This function shows a "Hello World" without an template view
{
echo 'Hello World';
}
function hello()
{
$user = CFactory::getUser($userid);
$tmpl = new CTemplate( ); //Call a template file
echo $tmpl->set ( 'user', $user )
->fetch ( 'hello' ); // Returns the templates/default/hello.php file
}
}
File templates/default/hello.php:
<?php defined('_JEXEC') or die();?>
<h2> This is an example </h2>
<div class="container">
<p> Hello, <?php echo $user->name; ?></p>
</div>
That's all!
I may have made this as a comment to the answer given by #Thavia-Farias in 2013, but my reputation is not high enough to comment. The content of my answer will restate her information along with crucial new information, corrections, and enhancements according to my experience using Jomsocial 4.2.1:
First off, the controllers/hello.php as provided by #Thavia-Farias has an error: In both the function helloWorld() and function helloWorld()function hello(), the final line is echo $view->get('helloWorld');, but the function in function hello() should be echo $view->get('hello');. As it stands, both *index.php?option=com_community&view=hello&task=helloworld and index.php?option=com_community&view=hello&task=hello will both call the helloworld view, rather than the second one calling the hello view as it should.
Also, in my experience, rather than putting the template at the path /templates/default/hello.php, I put it at /templates/customtemplatename/html/com_community/layouts if you are using a cusomt template or /components/com_community/templates/jomsocial/layouts/ if you are using the default jomsocial template.
create /components/com_community/controllers/hello.php:
<?php
defined('_JEXEC') or die();
class CommunityHelloController extends CommunityBaseController
{
public function renderView($viewfunc, $var = NULL) {
$my = CFactory::getUser();
$jinput = JFactory::getApplication()->input;
$document = JFactory::getDocument();
$viewType = $document->getType();
$viewName = $jinput->get('view', $this->getName());
$view = $this->getView($viewName, '', $viewType);
echo $view->get($viewfunc, $var);
}
function helloWorld()
{
$this->renderView(__FUNCTION__);
}
function hello()
{
$this->renderView(__FUNCTION__);
}
function display($cacheable = false, $urlparams = false) {
$this->renderView(__FUNCTION__);
}
}
?>
create /var/www/html/components/com_community/views/hello/view.html.php:
<?php
defined('_JEXEC') or die('Restricted access');
jimport ( 'joomla.application.component.view' );
class CommunityViewHello extends CommunityView {
function helloWorld() //This function shows a "Hello World" without an template view
{
echo 'Hello World';
}
function display() //This function what happens when the hello view is called without a task
{
echo 'welcome to the main landing page for the hello view! There is nothing else shown here besides this message.';
}
function hello()
{
echo $tmpl->fetch('hello');
}
}
As you can see, if you want your view to have a default view even when no task is called, similar to what happens with /index.php?option=com_community&view=groups then you will want to name a task as function display in the controller and in the view.
finally, create /components/com_community/templates/jomsocial/layouts/hello.php:
<?php defined('_JEXEC') or die();?>
<h2> This is an example </h2>
<div class="container">
<p> Hello, <?php echo $my->name; ?></p>
</div>
$my was defined way back in the controller! When your views and tasks group big enough, you will have different files for each task. The tasks files are with the fetch function in the view.html.php.
$tmpl = new CTemplate( ); //Call a template file
echo $tmpl->set ( 'vars1', $vars1)
echo $tmpl->set ( 'vars2', $vars2)
echo $tmpl->set ( 'vars3', $vars3)
->fetch ( 'hello' );
calls the /components/com_community/templates/jomsocial/layouts/hello.php file.
Using ->fetch ( 'hello.greeting' ); calls /components/com_community/templates/jomsocial/layouts/hello.greeting.php.
If you want to create a new directory for these then ->fetch ( 'hello/create' ); calls
/components/com_community/templates/jomsocial/layouts/hello/create.php
If you want to create menu items and aliases for your new components, then you need to create a new file (as well as a second and modify a third if you want to do pass menu-item defined parameters to your tasks):
create file: /components/com_community/views/hello/metadata.xml:
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<view title="Groups">
<message>
<![CDATA[
Hello view
]]>
</message>
<options var="task">
<default name="Hello" msg="displays landing page" />
<option value="hello" name="- one greeting" msg="Display detail page of one greeting" />
<option value="helloWorld" name="- helloworldview" msg="Display whatever you have in the hello world task" />
</options>
</view>
<state>
<name>Hello Groups Layout</name>
<description>Hello Groups listings</description>
</state>
</metadata>
This file will add items to the "community" section of the menu in the administrator menu panel. The option values are the names of the tasks. The option without a value that is uses the default tag will pull up the display function described earlier.
If you need to add parameters to the file, then you need to do something a bit complicated:
create /components/com_community/views/hello/tmpl/default.xml:
<?xml version="1.0" encoding="utf-8"?>
<metadata>
<layout title="Name" option="View">
<message>
</message>
</layout>
<fields name="params">
<fieldset
name="basic"
label="Selected Group">
<field
name="item_id"
query="SELECT `id`, `name` FROM #__community_groups_category WHERE ORDER BY `id`"
type="sql"
key_field="id"
value_field="name"
label="Associated Group"
require="true"
description="Select the jomsocial group whose hello task this will be associated with">
</field>
</fieldset>
</fields>
</metadata>
This will create a tab wherein users can specify one group out of available groups in the database. It will assign the id of the group to the parameters field in the #__menu database table in the params colums JSON object as the value for the item_id key. In order for your view to use that value when rendering the page, include the following code in views/hello/view.html.php:
$mainframe = JFactory::getApplication();
$jinput = $mainframe->input;
$document = JFactory::getDocument();
// Get category id from the query string if there are any.
$groupId = $jinput->getInt('group', 0);
// Load the parameters.
$params = $mainframe->getParams();
$params_array = $params->toArray();
if (isset($params_array['item_id']))
{
$groupId = $params_array['item_id'];
}
In this way, your task can receive the necessary specifics from either the URL if given from within your component(option=com_community&view=hello&task=hello&groupid=5), or from being called by a main menu or jomsocial toolbar item drawing of the stored parameters in the menu database table for that menu item.
The options and tab that you create here will be visible for all menu items of this task. If you want different tabs for different menu options, you will have to create entirely different views. having everything in one view may lead to unused and potentially misleading tabs where values can be set by your users but that will not or should not be used by the actual task specified by the user.
Forgive me for not testing every line of this code in an integrated component. I have done all of these functions in my view but have abridged my code which was built with initial guidance from #Thavia-Farias's answer. While it is more clear than posting my extensive code, it has not been tested in it's current form for functionality. Be sure to check your php error logs to debug your project. I have to log in as root (sudo su) and check with nano /var/log/mysqld/error_log on my system. Good luck!
I've a module (named Brèves) and in the index I need to be showed to columns: the first one shows only entries with an image; the second one shows entries without image. the thing is that I only want to be show the last 4 entries in each column. How can I do it? thank you
Symfony is a web application framework written in PHP which follows the model-view-controller (MVC) paradigm.
Your controller:
In your action:
// Action (controller) - apps/frontend/modules/youmudule/actions/actions.class.php
public function executeName(sfWebRequest $request)
{
$this->entries = Doctrine_Core::getTable('YouTableName') ->getEntry();
}
Your model:
Plase where you put all your "query"
// Model - lib/model/doctrine/YourtablenameTable.class.php
public function getEntry()
{
$q = $this->createQuery('a')
->addORDERBY ('created_at DESC')
->limit(4);
return $q->execute();
}
Your view:
// apps/frontend/modules/youmudule/templates/nameSuccess.php
<?php foreach ($entries as $entry): ?>
$entry->getSomeData()
<?php endforeach; ?>
Read this tutorial
I am trying to add a piece of code at the begining of every page in a Drupal site.
Since I have more than one page template, I want to do this programatically... but am not succeeding.
I am still new and, though I get the gist of hooks, theme functions, and the such, I just can't figure the correct way to achieve this.
So far I've overriden the theme_preprocess_page(&$vars) to add the necessary css and js:
function mytheme_preprocess_page(&$vars) {
if(condition) {
drupal_add_js(drupal_get_path('module', 'mymodule').'/js/modal.js');
}
}
How can I now add html code in every drupal page, preferably just after the opening bodytag or in any other starting section, via a function in the template.phpfile?
Thank you
In your preprocess function, any variable set, will be available in your page.tpl.php file.
function mytheme_preprocess_page(&$vars) {
if (condition) {
$vars['foo'] = '<div id="bar">TEST</div>';
}
}
then, in your page templates:
<body>
<?php print !empty($foo) ? $foo : ''; ?>
...
Had a look at https://www.drupal.org/project/google_tag. This is how they did it:
/**
* Implements hook_page_alter().
*
* Adds a post_render callback
*/
function MYMODULE_page_alter(&$page) {
$page['#post_render'][] = 'MYMODULE_CALLBACK';
}
/**
* Implements callback_post_render().
*
* Inserts JavaScript snippet immediately following the opening body tag.
*/
function MYMODULE_CALLBACK(&$children, $elements) {
$script = '<script type="text/javascript">console.log(\'hello world\');</script>';
// Insert snippet after the opening body tag.
$children = preg_replace('#<body[^>]*>#', '$0' . $script, $children, 1);
return $children;
}
This should keep you from having to modify any code in your templates:
function MY_MODULE_page_build(&$page)
{
$page['page_bottom']['my-markup'] = array('#markup' => '<div>My Markup Here</div>');
}
My approach finally was overriding the first rendered block in the page, in my case the Language Switcher.
Since I already was overriding it to customize it, it wasn't too much of a big deal, but it is anyway an ugly way to achieve that.
<?php
function mytheme_languageswitcher($links) {
// inserting this here like a virus in a dna strip
if(condition) {
drupal_add_js(drupal_get_path('module', 'mymodule').'/js/modal.js');
}
// the *real* code for the language switcher override
}
?>
Since the Language Switcher is rendered in every page, it works. The day the language switcher stops being displayed for whatever reason, this solution will FAIL.
You can add it to the footer with an option passed into the drupal_add_js function.
function THEMENAME_preprocess_page(&$variables) {
if (condition) {
drupal_add_js(drupal_get_path('theme', 'THEMENAME') . '/PATH/TO/FILE.js', array('scope' => 'footer'));
}
}
This will end up printing just before the closing body tag via the $page_bottom variable in the template html.tpl.php.
Another way to do it is to use the native drupal methods drupal_add_js and drupal_get_js.
// first, add your JS with a custom "scope" (before process phase)
<?php
drupal_add_js($tag_js, array(
'type' => 'inline',
'group' => JS_GROUP_TAGS,
'every_page' => TRUE,
'scope' => 'body_start',
'weight' => 1,
));
?>
// ugly way : add the drupal_get_js directly into html.tpl.php (but for testing, it's useful):
<body>
<?php print drupal_get_js('body_start'); ?>
...
</body>
// A cleaner way : use an intermediate variable, in a process method
// Exactly like template_process_html, in theme.inc
<?php
function MYMODULEORTHEME_process_html(&$variables){
$variables['js_body_start'] .= drupal_get_js('body_start');
}
?>
Enjoy :)