How to embed node add form in a block?
I have tried the following but it does not work. "free_listing2_node_form" is the form_id of the node add form I want to embed in this block.
If the approach below is right, I suspect problem in this statement
$block['content'] = drupal_get_form('free_listing2_node_form');
Any help / direction is much appreciated!
<?php
function freelisting2_block_info() {
$blocks['neil_recent'] = array(
'info' => t('neil_Recent content'),
);
return $blocks;
}
function freelisting2_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'neil_recent':
if (user_access('access content')) {
$block['subject'] = t('Recent content');
$block['content'] = drupal_get_form('free_listing2_node_form');
}
break;
}
return $block;
}
?>
(I am using Drupal 7)
Try to use this:
$block['content'] = render(drupal_get_form('free_listing2_node_form'));
I didn't test it.
Related
I found this code online and I would like to implement it. However, I have never worked with hook functions.
My question is when I put this code into a brand new php file ex: uc_microcartTest.php .
How do I call this new php file and get the results to show like this?
/**
* Implementation of hook_block().
*/
function uc_microcart_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
case 'list':
$blocks[0] = array(
'info' => t('Micro-sized cart block for page header.'),
// This block cannot be cached, because anonymous
// sessions can have differing cart contents.
// To improve this, see drupal.org/project/uc_ajax_cart
'cache' => BLOCK_NO_CACHE,
);
return $blocks;
case 'view':
if ($item_count = uc_cart_get_total_qty()) {
$block = array();
$block['subject'] = '';
$block['content'] = theme('image',
drupal_get_path('module', 'uc_cart') .'/images/cart-full.png');
$block['content'] .= format_plural($item_count,
'My cart: 1 item', 'My cart: #count items');
$block['content'] = l($block['content'], 'cart', array('html' => TRUE));
return $block;
}
break;
}
}
Although it is possible to call the functions from external PHP files but I recommend you follow the "Drupal way":
Create your own custom module
Implement hook functions in <your_module_name>.module file.
Enable your module
You should also read more about how hooks work in Drupal here
I'm trying to follow the instructions on this docs page, but I seem to be missing something:
https://docs.joomla.org/Supporting_SEF_URLs_in_your_component
The URI that needs to be corrected is: index.php?com_component&view=legal&page=customermasteragreement
It seems like the routing function should be simple, but the page is just displaying default instead of the sub-view.
Here's my current code:
function ComponentBuildRoute(&$query)
{
$segments = array();
if (isset($query['view'])) {
$segments[] = $query['view'];
unset($query['view']);
}
if (isset($query['page'])) {
$segments[] = $query['page'];
unset($query['page']);
}
return $segments;
}
function ComponentParseRoute($segments)
{
$vars = array();
switch($segments[0])
{
case 'legal':
$vars['view'] = 'legal';
break;
case 'customermasteragreement':
$vars['page'] = 'customermasteragreement';
break;
}
return $vars;
}
Update
This code works to display the subpage, but it gives me a URI like: legal-agreements/legal?page=customermasteragreement
class ComponentRouter extends JComponentRouterBase {
public function build(&$query) {
$segments = array();
$view = null;
if (isset($query['view'])) {
$segments[] = $query['view'];
$view = $query['view'];
unset($query['view']);
}
if (isset($query['id'])) {
if ($view !== null) {
$segments[] = $query['id'];
} else {
$segments[] = $query['id'];
}
unset($query['id']);
}
return $segments;
}
public function parse(&$segments) {
$vars = array();
// View is always the first element of the array
$vars['view'] = array_shift($segments);
return $vars;
}
}
EDIT 2
If it helps, here's my model and views
models/legal.php
// import Joomla modelitem library
jimport('joomla.application.component.modelitem');
class ComponentModelLegal extends JModelItem {
public function __construct($config = array())
{
JLoader::register('ComponentHelper', JPATH_COMPONENT_ADMINISTRATOR . '/helpers/component.php');
parent::__construct($config);
}
/**
*
* #return string
*/
public function getLegal() {
$app = JFactory::getApplication();
$page = $app->input->get('page', '', 'STRING');
if ($page) {
ComponentHelper::add('type', $page); //This is an API request to an external service, returning JSON formatted data
$legal = ComponentHelper::getData('commons/legal-agreements.json', TRUE);
if (isset($legal[0]['status'])) {
JError::raiseError(400, $legal[0]['ERROR']);
return false;
} else {
if (!isset($this->legal)) {
$this->legal = $legal;
}
return $this->legal;
}
}
}
}
views/legal/view.html.php
class ComponentViewLegal extends JViewLegacy {
function display($tpl = null) {
// Assign data to the view
$this->legal = $this->get('legal');
// Check for errors.
if (count($errors = $this->get('Errors'))) {
JLog::add(implode('<br />', $errors), JLog::WARNING, 'jerror');
return false;
}
// Display the view
parent::display($tpl);
}
}
views/legal/tmpl/default.php
$page = JRequest::getVar('page');
$pages = array(
'resellermasteragreement',
'customermasteragreement',
'resellerdomainagreement',
'customerdomainagreement',
'resellerwebserviceagreement',
'customerwebserviceagreement',
'resellerdigicertagreement',
'customerdigicertagreement',
'registraragreement',
'customerhostingproductagreement',
'resellerhostingproductagreement'
);
?>
<div class="col-lg-12">
<?php
echo in_array($page, $pages) ? $this->loadTemplate('legal') : $this->loadTemplate('home');
?>
</div>
views/legal/tmpl/default_legal.php
$page = JRequest::getVar('page');
echo nl2br(htmlspecialchars($this->legal[$page]['defaultagreement'], ENT_NOQUOTES, "UTF-8"));
?>
<a type="button" class="btn btn-primary" href="<?php echo JROUTE::_("index.php?option=com_component&view=legal"); ?>">Back</a>
EDIT 3
This works because I wasn't navigating directly to the page from a menu item. There's a top level menu, then a page a links to the agreements. I have a hidden menu to each item, but that wasn't the link I followed.
$app = JFactory::getApplication()->getMenu();
$customermaster = $app->getItems( 'link', 'index.php?option=com_component&view=legal&page=customermasteragreement', true );
JRoute::_('index.php?Itemid='.$customermaster->id);
You have one view but you're not actually set the page argument correctly when you catch a view argument, but you treat page as some king of another view. Can you please try this and check if it works:
function [componentname]ParseRoute($segments)
{
$vars = array();
switch($segments[0])
{
case 'legal':
$vars['view'] = 'legal';
$vars['page'] = $segments[1];
break;
}
return $vars;
}
Also the functions ComponentBuildRoute, ComponentParseRoute must have your components name instead of Component at the beginning.
EDIT
[componentname]BuildRoute and [componentname]ParseRoute are deprecated, you should stick the the class that extends JComponentRouterBase as you have it in your updated second example. Try this one here and see if it works:
class ComponentRouter extends JComponentRouterBase {
public function build(&$query) {
$segments = array();
$view = null;
if (isset($query['view'])) {
$segments[] = $query['view'];
$view = $query['view'];
unset($query['view']);
}
if (isset($query['page'])) {
$segments[] = $query['page'];
unset($query['page']);
}
return $segments;
}
public function parse(&$segments) {
$vars = array();
// View is always the first element of the array
$vars['view'] = array_shift($segments);
if(count($segments) > 0)
$vars['page'] = array_shift($segments);
return $vars;
}
}
EDIT 2
I have a test Joomla 3 site with a simple component named Ola.
Non SEO component URL: http://j3.dev.lytrax.net/index.php?option=com_ola&page=test_page&view=ola
URL generated by JRoute before Router class added to router.php: http://j3.dev.lytrax.net/index.php/component/ola/?page=test_page&view=ola
SEO URL returned by JRoute::_('index.php?option=com_ola&page=test_page&view=ola'); after creating router.php and used the Router class above: http://j3.dev.lytrax.net/index.php/component/ola/ola/test_page
If you visit the links, you'll see that all are working and you can even see the page, view and JRoute results of the calling component Ola.
Unless I've completely misunderstood Joomla (I've been developing with it for four five years now), there is no "sub-view" concept implemented. Trying to use the idea is what's getting you into trouble I think.
I'm astounded that the idea of using a visual debugger is so unpopular.
Get yourself something like Netbeans (there are others too), set up a local web and database server, and watch the code run. Very (well, relatively very) quickly you'll start to understand how the structure hangs together.
You may put code in your view that picks up the extra params you've set and displays or hides things accordingly, but there is no "sub-view".
Is the hook_block has been changed? the following is drupal 6 example i have found on internet(http://eureka.ykyuen.info/2010/11/10/drupal-create-a-block/), there is no block shown in admin/structure/block,
* Implementation of hook_block().
*/
function custom_block($op = 'list', $delta = 0, $edit = array()) {
switch ($op) {
//Define the block
case 'list':
$blocks[0]['info'] = t('Block Info');
$blocks[0]['cache'] = BLOCK_NO_CACHE;
return $blocks;
case 'configure':
//TODO: block configurable parameters
$form = array();
return $form;
case 'save':
//TODO: save new configuration
return;
//Display the block
case 'view':
$block['subject'] = t('Block Subject');
$block['content'] = 'Block Content';
return $block;
}
}
it seems that, hook_block in drupal 7 has been changed, how to rewrite about code? anyone can provide hints/direction to me? thank you very much.
In Drupal 7, your implementation of hook_block() would be changed to:
/**
* Implements hook_block_info().
*/
function custom_block_info() {
$blocks = array();
$blocks['list'] = array(
'info' => t('Block Info'),
'cache' => DRUPAL_NO_CACHE,
);
return $blocks;
}
/**
* Implements hook_block_view().
*/
function custom_block_view($delta = '') {
$block = array();
switch ($delta) {
case 'list':
if (user_access('access content')) {
$block['subject'] = t('Block Subject');
$block['content'] = 'Block Content';
}
break;
}
return $block;
}
Your code works for Drupal 6.The implementation of hook_block is changed in Drupal 7.
In Drupal 7 there are different hooks that should be used to serve your purpose.
hook_block_configure
hook_block_info
hook_block_save
hook_block_view
Check more about hook_block here
The following code is a Drupal block made in php.
1) How can I implement more then one item? now i have test1 but i want test1, test2, test3 and test5.
2) how can i link a title for example test1 to my admin/settings/ menu? I want to link an item to node_import in Drupal.
function planning_block($op='list', $delta=0, $edit=array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Stage administration block');
return $blocks;
case 'view':
$blocks['subject'] = t('Stage administratie');
$blocks['content'] = 'test';
return $blocks;
}
}
If you refer to the documentation of hook_block, you can declare several block inside one hook.
The $delta argument is here to help you differenciate which block your are rendering.
About your links in the title, just use the l() function when you are setting the $block['subject'] value.
Example:
function planning_block($op='list', $delta=0, $edit=array()) {
switch ($op) {
case 'list':
$blocks[0]['info'] = t('Stage administration block 1');
$blocks[1]['info'] = t('Stage administration block 2');
return $blocks;
case 'view':
switch ($delta) {
case 0:
$blocks['subject'] = t('Stage administratie');
$items = array(
l('Item 1', 'admin/settings/1'),
l('Item 2', 'admin/settings/2'),
);
$blocks['content'] = theme_item_list($items);
return $blocks;
case 1:
$blocks['subject'] = l('admin/settings/2', t('Stage administratie 2'));
$blocks['content'] = 'test 2';
return $blocks;
}
}
}
You can either create multiple blocks as shown in Artusamak's answer, or you can simply add more content to $blocks['content'] if you want it in a single block.
$blocks['content'] = l('admin/settings/1', 'test 1') . ' ' . l('admin/settings/2', 'test 2');
Note, if you just want a list of fixed links, you can do that by creating a menu and adding links to it. Every menu is automatically exposed as a block. No custom code required.
Well, I keep improving my form generation classes and stuck in returning all the country elements in country_data array. Only first two elements is displaying on dropdown options.
Here is dropdown class:
//drop down form class
class DropDown
{
function __construct ($form, $field_label, $field_name, $field_desc, $dropdown_data, $locale){
$this->form = $form;
$this->field_label = $field_label;
$this->field_name = $field_name;
$this->field_desc = $field_desc;
$this->dropdown_data = $dropdown_data;
$this->locale = $locale;
}
function getNotRequiredData(){
global $notReqArry;
return $notReqArry[$this->locale];
}
function getValue(){
return $_POST[$this->field_name];
}
function option(){
foreach ($this->dropdown_data as $key=>$value){
return $options = sprintf('%s',$key,$value);
};
}
function dropdown(){
return $select_start = "field_name\">$this->field_desc".$this->option()."";
}
function getLabel(){
$non_req = $this->getNotRequiredData();
$req = in_array($this->field_name, $non_req) ? '' : '*';
return $this->field_label ? $req . $this->field_label : '';
}
function __toString(){
$id = $this->field_name;
$label = $this->getLabel();
$field = $this->dropdown();
return 'field_name.'">'.$label.''.$field.'';
}
}
And I use extra function for extra options:
function generateForm ($lang,$country_list){
switch($lang)
{
case 'en-US':
//create EN web form
echo $countryField = new DropDown ($form, 'Country', 'form_country', '--Select Country--', $country_list, 'en-US');
break;
case 'fr-FR':
//create FR web form
break;
case 'de-DE':
//create DE web form
break;
case 'ja-JP':
//create JA web form
break;
default:
//create default web form
print('foooo');
};
}
And I call generateForm fun at the bottom of page.
$lang='en-US';
echo generateForm ($lang,$country_list);
At the previous question, one expert mentioned the $key and $value in foreach are not object, but I do not understand what I need to more logic on here. Yeah, I really new at PHP and just have short experience on AS. I need help.
Thanks.
Your options function is trying to iterate through all of the options available but always returning just the first one. Use this instead:
function options(){
$options = '';
foreach ($this->dropdown_data as $key=>$value){
$options .= sprintf('<option value="%s">%s</option>',$key,$value);
};
return $options;
}
It's a surprise you have two elements in your dropdown, according to your code, you should have only one. since your function
function options(){
foreach ($this->dropdown_data as $key=>$value){
return $options = sprintf('%s',$key,$value);
}; }
Returns only the first option it gets out of the array, and exits the cycle. Collect all the items together and return in a bunch or call the option extractor from within some outer cycle and it will work.