i've been using and studying Collin Williams template plugin (http://williamsconcepts.com/ci/codeigniter/libraries/template/reference.html#manipulation) and i've already posted this issue on CI's forum but i think the last post was last year maybe its not being monitored by Colllin or wat but i guess i'll just have to post this here maybe you guys can help.
Original Post on CI Forum
Hello Collin,
I’ve been studying your template plugin lately, as i was following your guide,
i came across this line of code
$data = array('name' => 'John Smith', 'birthdate' => '11/15/1950');
$this->template->write_view('content', 'user/profile', $data, TRUE);
it was a bit confusing whether in the view files, like
mymastertemplate.php for example, how do i accessthe $data array, does
it have to be $content defined by that first param. a region, or by
$name and $birthdate? ... cuz’ it says there $content will display the
data array? its a bit confusing. Hope you could enlighten me.
Basically thats my problem.
On Template.php library we can see function write_view(). Now, focus on $data = NULL. Now then finds a file of existed data on APPPATH.'views/'.$suggestion.'.php' so I think that $args[0] should be a file which is loaded and break it, than loaded a view template on $data.
function write_view($region, $view, $data = NULL, $overwrite = FALSE)
{
$args = func_get_args();
// Get rid of non-views
unset($args[0], $args[2], $args[3]);
// Do we have more view suggestions?
if (count($args) > 1)
{
foreach ($args as $suggestion)
{
if (file_exists(APPPATH .'views/'. $suggestion . EXT) or file_exists(APPPATH .'views/'. $suggestion))
{
// Just change the $view arg so the rest of our method works as normal
$view = $suggestion;
break;
}
}
}
$content = $this->CI->load->view($view, $data, TRUE);
$this->write($region, $content, $overwrite);
}
In another way, $data should be as array which will response for View template data on Codeigniter library (standard view of CI: $this->CI->load->view(...))
$data = array('name' => 'John Smith', 'birthdate' => '11/15/1950');
$this->template->write_view('content', 'user/profile', $data, TRUE);
On template file '/user/profile.php' use as example:
HTML/PHP template file profile.php:
Your name: <?php echo $data["name"]; ?>
Your name: <?php echo $data["birthdate"]; ?>
And as I see, a CONTENT var must be an ARRAY due to documentation...
$template['default']['regions'] = array(
'header' => array(
'content' => array('<h1>Welcome</h1>','<p>Hello World</p>'), ### <----- AS EXAMPLE
'name' => 'Page Header',
'wrapper' => '<div>',
'attributes' => array('id' => 'header', 'class' => 'clearfix')
)
);
Regions must be defined as template, so if you didn't have header region that didn't work:
$template['default']['regions'] = array(
'header',
'content',
'footer',
);
!!!!!
Simply, he can't acces private access variable _ci_cached_vars which is stored data like $name. RELATED TOPIC: CodeIgniter shared data between calls to load->view
Related
In my Symfony 3.3 application, I have built a block service using SonataBlockBundle. Now I want to pull some other field values from the page on which the block lives. In other words, I want to do something like this:
public function configureSettings(OptionsResolver $resolver)
{
$pageRepository = $this->doctrine->getRepository('ApplicationSonataPageBundle:Page');
$pageId = someMagicalMethodCall();
$page = $repository->findOneBy(['id' => $pageId]);
$images = $page->getImageUrls;
$resolver->setDefaults(array(
'content' => 'Some custom content',
'images' => $images,
'template' => 'AppBundle:Block:block_media.html.twig',
));
}
Is this possible? If so, what would I put in place of someMagicalMethodCall in the block above?
It is possible, but you need to inject additional service in your block - CmsManagerSelector. Then in your configureSettings you need to retrieve the proper manager and get the current page instance from it. For example in your code:
public function configureSettings(OptionsResolver $resolver)
{
$cmsManager = $this->cmsManagerSelector->retrieve();
// $page will be the Page object already, no need to call doctrine repository. Reference: https://github.com/sonata-project/SonataPageBundle/blob/3.x/src/CmsManager/BaseCmsPageManager.php#L38
$page = $cmsManager->getCurrentPage();
$images = $page->getImageUrls;
$resolver->setDefaults(array(
'content' => 'Some custom content',
'images' => $images,
'template' => 'AppBundle:Block:block_media.html.twig',
));
}
Thanks to Jakub Krawczyk and a mentor, I found this page:
Getting instance of container in custom sonata block
... which led me to another way of getting the page related to a block, from within the execute() method. So I now have the following code, which serves me well:
public function execute(BlockContextInterface $blockContext, Response $response = null)
{
$page = $blockContext->getBlock()->getPage();
$localImages = $page->getImages();
$imageProvider = $this->provider;
foreach ($localImages as $key => $image) {
$publicImages[$key]['url'] = $imageProvider->generatePublicUrl($image, 'reference');
$publicImages[$key]['name'] = $image->getName();
}
$settings = $blockContext->getSettings();
$settings['images'] = $publicImages;
return $this->renderResponse($blockContext->getTemplate(), array(
'block' => $blockContext->getBlock(),
'settings' => $settings,
), $response);
}
Again, thanks to all involved.
sensei. can you help me with my code? i wan to redirect page to the same page after edit it.
here is my controller
function edit_book(){
$editBook = array(
'id' => $this->input->post('id'),
'book_title' => $this->input->post('book_title'),
'category' => $this->input->post('category'),
'author' => $this->input->post('author')
):
$this->m_profile->edit_book($data1, $data, 'book_data');
redirect('app/book')
}
suppose im editing the data from page number 3. how can i redirect to page 3 (app/book/3) again after submit the edited data?
i have try to solve it by get URI value(suppose the page is app/book/3)<-- i need this '3'to put in redirect code. so i implement this code from many stackeroverflow answer, hope to get some array i can use
$url = "//{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}";
$escaped_url = htmlspecialchars( $url, ENT_QUOTES, 'UTF-8' );
$query_str = parse_url($escaped_url, PHP_URL_QUERY);
parse_str($query_str, $query_params);
print_r($query_params);
but it results array()or null. anyone can help me. thanks
function edit_book(){
$id = $this->input->post('id');
$editBook = array(
'id' => $this->input->post('id'),
'book_title' => $this->input->post('book_title'),
'category' => $this->input->post('category'),
'author' => $this->input->post('author')
):
$this->m_profile->edit_book($data1, $data, 'book_data');
$url = base_url('app/book/'.$id);
redirect( $url)
}
function book($id = NULL)
{
//get record using $id;
}
try below code me be solve u r issue. i have pass id in url.
In the controller need to pass the last value like "3" as argument so that any edited value can be pass to the controller. And then after editing the code you can redirect by the php header function with the passed value as argument to redirect on the same page like this example:
<?php
function edit_book($id){
Your all edited codes here.
// To redirect to the same page with avalue
redirect('same_controller/method/'.$id, 'refresh');
}
?>
I am using codeigniter I have an edit page which shows me all information of a vacancy.
The (Vacancy) controller method to load this view looks like this, it makes sure all data is preloaded.
public function editVacancy($vacancyid)
{
$this->is_logged_in();
$this->load->model('vacancy/vacancies_model');
// Passing Variables
$data['title'] = 'Titletest';
$data['class'] = 'vacancy';
$orgadminuserid = $this->vacancies_model->getOrgAdminUserId($vacancyid);
if ((!is_null($orgadminuserid)) && ($this->auth_user_id == $orgadminuserid[0]->user_id)) {
$data['vacancyid'] = $vacancyid;
$data['vacancy'] = $this->vacancies_model->get($vacancyid);
$data['test'] = $this->session->flashdata('feedbackdata');
$partials = array('head' => '_master/header/head', 'navigation' => '_master/header/navigation_dashboard', 'content' => 'dashboard/vacancy/edit_vacancy', 'footer' => '_master/footer/footer');
$this->template->load('_master/master', $partials, $data);
}
}
In this view i have different forms for updating different sections.
Every form submit goes to a different method in my 'Vacancy' controller.
public function saveGeneralInfo()
{
$this->is_logged_in();
$this->load->model('vacancy/vacancies_model');
$vacancyid = $this->input->post('vacancyid');
$vacancyUpdateData = $this->vacancies_model->get($vacancyid);
$result = $this->vacancies_model->update($vacancyid, $vacancyUpdateData);
if ($result) {
$feedbackdata = array(
'type' => 'alert-success',
'icon' => 'fa-check-circle',
'title' => 'Success!',
'text' => 'De algemene vacature gegevens zijn geupdate.'
);
$this->session->set_flashdata('feedbackdata', $feedbackdata);
redirect("dashboard/vacancy/editVacancy/" . $vacancyid);
}
}
}
Where I indicated in my code "//HERE ..." is where I would want the feedback message parameter to pass on to my 'main controller method' which loads the view with the prefilled data. (editVacancy).
Is there a clean way to do this?
EDIT:
I tried using flashdata, i updated the code to have the flashdata inserted.
However when i do a var_dump($test); in my view, it remains null.
EDIT 2:
I noticed when i put my $_SESSION in a variable in my editVacancy controller method (which is being redirected to) and var_dump it in my view that this does not contain the ci_vars with the flashdata in.
Instead of using set_header you can use simple redirect function for it.
redirect("dashboard/vacancy/editVacancy/".$vacancyid);
We created a few custom custom modules for a Drupal site and we have the following issue : each time we create or update a content (whatever it is), the content generated by the custom modules disappears. We have to clear all caches to get this content to appear again.
As it is our first experience with Drupal, we are certainly missing something but we don't know what.
Any help would be greatly appreciated!
Below is the code of one of these custom modules:
File website_actualites.module
<?php
/**
* Implements hook_block_info().
*/
function website_actualites_block_info() {
$blocks['website_actualites'] = array(
'info' => t('website_actualites'),
'cache' => DRUPAL_CACHE_PER_ROLE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function website_actualites_block_view($delta = '') {
$aDelta = explode('+', $delta);
$nbActualite = 2;
if (!empty($aDelta[1])) {
$nbActualite = $aDelta[1];
}
$block = null;
switch ($aDelta[0]) {
case 'website_actualites':
$block['content'] = _website_actualites_sweet_block_content($nbActualite);
break;
}
return $block;
}
/**
* Callback implemented by hook_block_view().
*/
function _website_actualites_sweet_block_content($nbActualite=2) {
$query = new EntityFieldQuery();
$query->entityCondition('entity_type', 'node')
->entityCondition('bundle', 'article')
->propertyCondition('status', 1)
->fieldCondition('field_mise_en_avant', 'value', 1)
->propertyOrderBy('created', 'DESC')
->range(0, $nbActualite)
->addMetaData('account', user_load(1));
$result = $query->execute();
// width col pour nb actu=2 : 4,8
// width col pour nb actu=4: 2,4,2,4
$colWidthEven=4;
$colWidthOdd=8;
if (4 == $nbActualite) {
$colWidthEven=2;
$colWidthOdd=4;
}
$data = array();
if (isset($result['node'])) {
$nids = array_keys($result['node']);
$items = entity_load('node', $nids);
$i=0;
foreach ($items as $item) {
$colWidth=$colWidthOdd;
if (0 == $i%2) {
$colWidth = $colWidthEven;
}
$i++;
$data[$item->nid] = array(
'title' => $item->title,
'tags' => isset($item->fielsTags['und'][0]) ? $item->fielsTags['und'][0] : '',
'body' => isset($item->body['und'][0]['value']) ? $item->body['und'][0]['value'] : '',
'image' => isset($item->field_image['und'][0]) ? $item->field_image['und'][0] : '',
'nid' => $item->nid,
'col-width' => $colWidth,
'alias' => drupal_get_path_alias('node/'.$item->nid)
);
}
}
$static_title = t('Static Title');
$static_content = 'static content';
return theme('website_actualites_output', array(
'title' => $static_title,
'content' => $static_content,
'data' => $data
)
);
}
File website_actualite-sweet--block.tpl.php
<?php
foreach ($data as &$row) {
$url = drupal_get_path_alias('node/' . $row['nid']);
$imageWrapper = file_stream_wrapper_get_instance_by_uri($row['image']['uri']);
if (is_object($imageWrapper) && is_callable(array($imageWrapper, 'getExternalUrl'))) {
$imageUrl = $imageWrapper->getExternalUrl();
print '<div class="col-sm-'.$row['col-width'].'">
<div class="img">
<img src="' . $imageUrl . '" class="img-responsive" alt="image description">
</div>
<p>' . $row['title'] . '</p>
</div>';
}
}
first of all there are several problems with this code. If you're going to use Drupal then you need to stick to Drupal coding standards. Avoid using camel casing for variables, instead you should use underscores. Also you should use 2 spaces for indentation.
In your block info function you define a single block however you assign it to an unassigned variable:
// Define $blocks first.
$blocks = array();
$blocks['website_actualites'] = array(
Next issue is that you call hook_block_view($delta), this call will be fired for every block delta, and regardless of the block delta you are setting it to null (also for future reference should be NULL):
$block = null;
So Drupal is building the render array for each block, and you are wiping all the data. You are doing this because you are trying to use the block delta as a way to pass a parameter into your block with a delta like website_actualites+10. Blocks aren't designed to work this way and delta's are meant to be static so that Drupal can keep track of them in the database and perform the correct caching according to your cache flag. You've defined the delta website_actualites in your hook_block_info() but Drupal has no knowledge or configuration information for any other block delta.
If you have a need for the same block to display varying quantities, then just define several blocks (in your hook_block_info()) and call the same helper function as you are now. If there is a requirement to configure the block then you should use the hook_block_configure() API
Now the data you are passing into your theme function, you are extracting field data from the node object using: $item->field_image['und'][0]. A couple of things here: fields are provided by the field module which has an extensive API to retrieve field data. You should use field_get_items() instead, which will return an array of all items associated with that field for that entity (as fields can be multi-valued) and handles the language for you. The other thing is never use 'und' use the constant LANGUAGE_NONE.
$body = field_get_items('node', $item, 'body');
$field_image = field_get_items('node', $item, 'field_image');
$field_tags = field_get_items('node', $item, 'field_tags');
$data[$item->nid] = array(
'title' => $item->title,
'tags' => $field_tags ? $field_tags : FALSE,
'body' => $body ? $body[0] : FALSE,
'image' => $field_image ? $field_image[0] : FALSE,
'nid' => $item->nid,
'col-width' => $colWidth,
'alias' => drupal_get_path_alias('node/'.$item->nid)
);
Another problem is that you set the $block['content'] to the result of theme('website_actualites_output', ...). Now this function will return markup, Drupal has many more calls in the chain before we need to render any markup, and the problem with calling this now is that you can't mutate your data at any point now in the build process.
In order to call theme('website_actualites_output', ...); You need to have defined this theme function in a hook_theme() call, presumably you have done this in another module, which is perfectly fine but remember to add a dependency on that module in your module.info file. What you really want to be doing is just adding instructions to the Drupal build, not markup:
return array(
'#theme' => 'website_actualites_output',
'#title' => $static_title,
'#content' => $static_content,
'#data' => $data
);
So then we come to your template, you have a lot of logic in your template and they are only intended for displaying content with markup, with ideally no, or minimal computation if any. A theme function has a hook_preprocess() call before hook_process(), and then the variables are passed into the template.
You are using this theme function to loop over the $data variable to display markup - ideally a theme function would be just that markup with the variables already processed and passed into it:
website-actualites-output--child.tpl.php
<div class="col-sm-<?php print $col_width; ?>">
<?php if ($image): ?>
<div class="img">
<?php print render($image); ?>
</div>
<?php endif; ?>
<?php if ($title): ?>
<p><?php print render($title); ?></p>
<?php endif; ?>
</div>
In this example your best bet would be to have a second theme function website_actualites_output__child with the above template.
/**
* Implements hook_theme().
*/
function website_actualites_theme() {
$templates = drupal_get_path('module', 'website_actualites') . '/templates';
return array(
'website_actualites_output__child' => array(
'path' => $templates,
'template' => 'website-actualites-output--child',
'variables' => array(
'col_width' => 4, // Default col width.
'image' => NULL,
'title' => NULL,
),
),
);
}
Then preprocess your current theme function:
/**
* Implements hook_preprocess_HOOK().
*/
function website_actualites_preprocess_website_actualites_output($variables) {
$processed_output = array();
$data = $variables['data'];
foreach ($data as $row) {
$image = array(
'#theme' => 'image',
'#path' => file_create_url($row['image']['uri']),
'#alt' => $image['alt'],
'#attributes' => array('class' => array('img-responsive')),
);
$processed_output[] = array(
'#theme' => 'website_actualites_output__child',
'#col_width' => $row['col-width'],
'#image' => array(
'#theme' => 'link',
'#path' => 'node/' . $row['nid'],
'#text' => $image,
'#options' => array('HTML' => TRUE),
),
'#title' => array(
'#theme' => 'link',
'#path' => 'node/' . $row['nid'],
'#text' => $row['title'],
),
);
}
// Reassign the $data variable so that we can just render() it.
// var_dump($processed_output) to get a better idea what's going on here.
$variables['data'] = $processed_output;
}
Then all you need to do in your current template is to:
<?php if ($data): ?>
<?php print render($data); ?>
<?php endif; ?>
And you can wrap this is whatever markup you like.
The Drupal learning curve is high, but remember that if someone else were to maintain this code after you, they would expect that you adhere to Drupal coding standards and followed the process upon which Drupal is built.
Further reading: Render Arrays in Drupal 7
Hope this information helps and makes it a bit clearer.
I'm using CakePHP to create a frontend UI for PowerDNS, using a MySQL backend. On the front page of the app I want to have a handful of widgets ('Quickly add a record', 'Quickly add a domain' etc.). One of the widgets I want is a paginated list of existing domains.
The index function in DomainsController.php looks like this:
public $paginate = array(
'fields' => array('id', 'name'),
'limit' => 25,
'order' => array( 'name' => 'asc' ),
'conditions' => array( "NOT" => array( "name LIKE" => "%.arpa" ) )
);
public function index() {
$domains = $this->paginate();
if ( $this->request->is('requested')) {
return $domains;
} else {
$this->set('domains', $domains);
}
}
I've created an element that looks like this:
<?php $domains = $this->requestAction('Domains/index'); ?>
<ol>
<?php foreach( $domains as $domain) :?>
<li>echo $domains['domain']['name']</li>
<?php endforeach; ?>
</ol>
<?php echo $paginator->numbers(); ?>
When I visit the front page, I get an 'Undefinied variable: paginator' error. I've tried using $this->Paginator->numbers() instead but that just gives me 'Undefined property: View::$Paginator'. Adding the 'Paginator' helper to PagesController.php doesn't help either - $this->Paginator becomes available but I get 'Undefined index: pageCount'.
Is it possible to do this kind of pagination from an element on home.ctp or am I going to have to do some custom JavaScript stuff?
EDIT
Now I'm getting somewhere: I changed my DomainsController index function to this:
public function index() {
$domains = $this->paginate();
$paginator = $this->params;
if ( $this->request->is('requested')) {
return compact( 'domains', 'paginator' );
} else {
$this->set('domains', $domains);
}
}
And added the following to the domainList.ctp element:
<?php
$result = $this->requestAction('Domains/index');
$domains = $result['domains'];
$this->Paginator->request = $result['paginator'];
?>
<ol>
<?php foreach( $domains as $domain) :?>
<li>echo $domains['domain']['name']</li>
<?php endforeach; ?>
</ol>
<?php echo $paginator->numbers(); ?>
$this->Paginator is now working properly and I can access all of its methods and properties and so on as normal. My problem now is that if I click on, say, '2', the browser navigates to /pages/home/page:2 but the domain list still shows page 1. Just need to figure out how to pass 'page:2' to the element. And AJAX-ify the whole thing so that I don't need to refresh the whole page.
Firstly, don't do this:
$domains = $this->requestAction('Domains/index');
It's expensive and not good practice and I'm not sure why you need to be doing it from your example.
Secondly, call your paginate like this:
$domains = $this->Paginate('Domain');
OK, I solved this problem, although my solution probably isn't very elegant.
DomainsController.php has a listDomains() function that looks like this:
public function listDomains() {
$domains = $this->paginate();
$paginator = $this->params;
if ( $this->request->is('ajax') ) {
$this->set( 'domains', $domains );
}
if ( $this->request->is('requested')) {
return array( 'domains' => $domains, 'paginator' => $paginator, 'paging' => $this->params['paging'] );
} else {
$this->set( 'domains', $domains );
}
}
home.ctp references an element called domainList.ctp. domainList.ctp, below, in turn uses requestAction() - I know, I know - to call the domainList() function above. Bequest the request is requested, an array containing the values of $domains and $paginator is sent back to the element.
domainList.ctp contains this code:
<?php
$result = $this->requestAction('Domains/listDomains', array('updateId' => 'domainList') );
$domains = $result['domains'];
$paginator = $result['paginator'];
$this->Paginator->request = $paginator;
$this->Paginator->options(array(
'update' => '#domainList',
'evalScripts' => true,
'url' => array('controller' => 'Domains', 'action' => 'listDomains', 'updateId' => 'domainList' ),
));
?>
Essentially what I'm doing here is manually re-populating $this->Paginator->request with the params that were originally sent to the DomainController's domainList() function. This lets me access the various paginator functions, like numbers(), prev() and next(), properly. It's a bit messy but guess what? It gets a little messier.
When you click on the links created by those paginator functions, the 'if ( $this->request->is('ajax') )' segment is executed and the div object on the page is updated with the contents of View/Domains/domainList.ctp instead of View/Elements/domainList.ctp. The contents of View/Domains/domainList.ctp is more or less the same as the corresponding element and the two have to be kep more or less syncronised. The difference is that we don't need to manually populate $this->Paginator:
<?php
$this->Paginator->options(array(
'update' => '#domainList',
'evalScripts' => true,
'url' => array('controller' => 'Domains', 'action' => 'listDomains', 'updateId' => 'domainList' ),
));
?>
Like I said, it's messy and inelegant but it worked for me. I'd be happy to know if anyone has a less kludgy way to do this.