I'm creating a website using Drupal 8. I want to create a menu item link that I could add HTML/Javascript code in it (I'm trying to display a widget that expands on click in the menu rather than displaying it in its own block next to the menu). The only way I could see to add a menu item is to link to a page.
You could work with a derivative. This lets you customize pretty much everything about it and control what is to be made. An example below:
Note: I am assuming you have a general knowledge of custom modules. If not follow this link
Create the following file in your custom module:
# my_module.links.menu.yml
my_module.custom_links:
deriver: \Drupal\my_module\Plugin\Derivative\CustomLinkDerivative
And now for the derivative class (Located under my_module/src/Plugin/Derivative)
<?php
namespace Drupal\my_module\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DeriverBase;
use Drupal\Core\Plugin\Discovery\ContainerDeriverInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
class CustomLinkDerivative extends DeriverBase implements ContainerDeriverInterface {
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static();
}
/**
* {#inheritdoc}
*/
public function getDerivativeDefinitions($base_plugin_definition) {
$links['custom_menulink'] = [
'title' => t('Custom menulink'),
'menu_name' => 'main',
'route_name' => 'entity.node.canonical',
'parent' => footer,
'route_parameters' => [
'node' => 1,
]
] + $base_plugin_definition;
return $links;
}
}
Note: Derivatives get triggered during rebuild of cache!
This just creates a link in the footer that directs to node 1. You can add all sort of stuff and logic to your liking. Hope this helps you :)
Related
I am new to Drupal, I have made a custom module using PHP, which shows List of Student's with Information, and want to call it, on click of Submenu item, named, student Info. Please guide me by step wise step procedure.
The starting place to look for generating a "page callback" (essentially making a url active in drupal) would be hook_menu. As suggested take a look at the documentation but a starting point to actually make your callback work would be this in a my_module.module file:
/**
* Implements hook_menu().
*/
function my_module__menu() {
$items = array();
$items['student-info'] = array(
'title' => 'Student Info', // This becomes the page title
'description' => 'Information about students.', // this is the link description
'page callback' => 'function_name_that_outputs_content', // this is the page callback function that will fire
'type' => MENU_CALLBACK, // this is the type of menu callback, there are several that you can use depending on what your needs are.
);
return $items; // make sure you actually return the items.
}
/**
* Output the page contents when someone visits http://example.com/student-info.
*/
function function_name_that_outputs_content() {
$output = 'My page content'
return $output;
}
I am trying to implement Dashboard widget class (found here: http://harpanet.com/programming/php/codeigniter/dashboard/index#installation) but it is giving me error Unable to load the requested class
I have tried to add this class in autoload as well as menually to my controller $this->load->library('dash') but this also giving the same error.
I have checked dash.php and found below method private function __example__() but can't understand what the developer is saying in comment.
class Dash
{
private function __example__()
{
/*
* This function is purely to show an example of a dashboard method to place
* within your own controller.
*/
// load third_party hArpanet dashboard library
$this->load->add_package_path(APPPATH.'third_party/hArpanet/hDash/');
$dash =& $this->load->library('dash');
$this->load->remove_package_path(APPPATH.'third_party/hArpanet/hDash/');
// configure dashboard widgets - format: type, src, title, cols, alt (for images)
$dash->widgets = array(
array('type'=>'oop', 'src'=>'test_dash', 'title'=>'Test OOP Widget', 'cols'=>3),
// if 'title' is set to FALSE, the title block is omitted entirely
// note: this is an 'html' widget but is being fed content from a local method
array('type'=>'html', 'src'=>self::test_method(), 'title'=>false, 'cols'=>3),
array('type'=>'file', 'src'=>'saf_inv.htm', 'title'=>'Safety Investigation'),
// multi-content widget - set widget title in outer array (also note use of CI anchor to create a link)
array('title'=>anchor('tz', 'TARGET ZERO'),
// sub-content follows same array format as single content widget
// 'img' content can also have an 'alt' text
array('type'=>'img', 'src'=>'saf_tzout.gif', 'alt'=>'Action Completed'),
array('type'=>'file', 'src'=>'saf_tz.htm'),
array('type'=>'file', 'src'=>'ave_close.htm', 'title'=>'Average Time to Close')
),
array('type'=>'file', 'src'=>'saf_meet.htm', 'title'=>'Safety Meeting'),
array('type'=>'file', 'src'=>'saf_acc.htm', 'title'=>'Accident Investigation'),
array('type'=>'file', 'src'=>'saf_hazmat.htm', 'title'=>anchor('hazmat', 'HAZMAT')),
array('type'=>'file', 'src'=>'saf_cont.htm', 'title'=>'Loss of Containment'),
array('type'=>'file', 'src'=>'saf_worksinfo.htm', 'title'=>'Works Information'),
// an action widget - 'clear' will generate a blank widget with a style of clear:both
array('type'=>'clear'),
// multi-content widget - width can be set using the 'cols' param in outer array
array('title'=>'RAG Report', 'cols' => 2,
array('type'=>'file', 'src'=>'saf_rag.htm'),
array('type'=>'img', 'src'=>'ProcSaf.gif')),
array('type'=>'file', 'src'=>'saf_chrom.htm', 'title'=>'Chrome checks'),
);
// populate the view variable
$widgets = $dash->build('safety');
// render the dashboard
$this->load->view('layout_default', $widgets);
}
...................
} // end of Dash class
Installation path is root/application/third_party/hArpanet/hDash/libraries/dash.php
How can I load this class to my system and use widgets?
You have to create a library that initialize the third party class:
for Eg:
--in library create a file named mydash.php --
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class MyDash
{
public function __construct()
{
require_once APPPATH.'third_party/hArpanet/hDash/libraries/dash.php';
}
}
load the library using:
$this->load->library('mydash');
then you are able to use the Dash class. Also able to load library in autoload.
Thank you...
Sorry to hear you were having problems, (I've only just noticed this SO entry). Thanks to ReNiSh for his workaround, much appreciated.
You do not however need to use the library approach to implement the 'require_once' of hDash. I have now written a walkthrough for getting hDash installed and running, which you can find here: http://harpanet.com/programming/php/codeigniter/dashboard/walkthrough
Also, note that as of yesterday, 3rd Feb 2014, hDash has been updated to version 1.2.
I am using PDF Parser from http://pdfparser.org/
I create files application/libraries/pdf.php
class Pdf
{
public function __construct()
{
require_once APPPATH."/third_party/pdfparser.php";
}
}
Then I create file application\third_party\pdfparser.php
if (!defined('pdfparser')) {
define('pdfparser', dirname(__FILE__) . '/');
require(pdfparser . 'pdfparser/autoload.php');
}
Last, I include PDF Parser Library in Directory => application\third_party\pdfparser
I have this widget:
<?php
class Search extends CWidget
{
public $dataProvider = null;
public function init()
{
$criteria = new CDbCriteria();
if ( isset($_GET['file']) ) {
$criteria->compare('fileName', $_GET['file'], true, 'OR');
$criteria->compare('tags', $_GET['file'], true, 'OR');
}
$this->dataProvider = new CActiveDataProvider("Files", array(
'criteria' => $criteria,
//'countCriteria' => $criteria,
'pagination'=>array(
// results per page
'pageSize'=>1,
),
));
}
public function run(){
$this->render('site/result', array(
'dataProvider' => $this->dataProvider,
'pages' => $this->dataProvider->pagination,
));
}
}
?>
how I can run this widget only when the search button is clicked, and how to view its result in the view reuslt that is located in the folder site
Maybe I misunderstood you, but I think you have a little confusion about widget. Avoid making a
lengthy answer, I made a model in below
Question 1: How I can run this widget only when the search button is clicked?
The idea is that you put the widget content into hidden div, and then add a jQuery script to show it after the search button is clicked
<div id='search-result' style="display:none"><?php $this->widget(...) ?></div>
<script>
$('#search-button-id').click({
// do something
$('#search-result').show();
})</script>
Question 2: how to view its result in the view reuslt that is located in the folder site?
Imao, I recommend you don't do this. The widget is independant part, you can use it for many specific purposes & many locations of your project, so just put a simple view for widget to render in its own, instead of trying to render a view outside widget's scope. It should look like below
/your-app/protected/extensions/widgets/search/Search.php
/your-app/protected/extensions/widgets/search/views/result.php // view for rendering
Does anyone know how can I add a custom product attribute with a widget renderer?
You can see this in Promo rules if you select SKU you'll got an Ajax popup with product selection.
so how would I go about it?
in :
$installer->addAttribute(Mage_Catalog_Model_Product::ENTITY...
In other words, how can I use a widget to select custom attribute values?
EDIT:
The scenario is as follows:
I would like to create a product attribute that will, upon a button click, open a product selection widget.
After the selection, the selected SKU's will go in in a comma delimited format.
This behavior can be seen in the catalog and shopping cart price rules.
If you filter the rule by SKU (SKU attribute must be enabled to "apply to rules"), you'll get a field and a button that will open the product selection widget.
Here is some thoughts that should get you going on the right track:
First, in a setup script, create your entity:
$installer->addAttribute('catalog_product', 'frontend_display', array(
'label' => 'Display Test',
'type' => 'varchar',
'frontend_model' => 'Test_Module/Entity_Attribute_Frontend_CsvExport',
'input' => 'select',
'required' => 0,
'user_defined' => false,
'group' => 'General'
));
Make sure to set the frontend_model to the model that you are going to use. The frontend model affects the display of the attribute (both in the frontend and the adminhtml sections).
Next, create yourself the class, and override one or both of the following functions:
public function getInputType()
{
return parent::getInputType();
}
public function getInputRendererClass()
{
return "Test_Module_Block_Adminhtml_Entity_Renderer_CsvExport";
}
The first (getInputType()) is used to change the input type to a baked in input type (see Varien_Data_Form_Element_* for the options). However, to set your own renderer class, use the latter function - getInputRendererClass(). That is what I am going to demonstrate below:
public function getElementHtml()
{
return Mage::app()->getLayout()->createBlock('Test_Module/Adminhtml_ExportCsv', 'export')->toHtml();
}
Here, to clean things up, I am instantiating another block, as the element itself doesn't have the extra functions to display buttons and the like.
Then finally, create this file:
class Test_Module_Block_Adminhtml_ExportCsv extends Mage_Adminhtml_Block_Widget
{
protected function _prepareLayout()
{
$button = $this->getLayout()->createBlock('adminhtml/widget_button')
->setData(array(
'label' => $this->__('Generate CSV'),
'onclick' => '',
'class' => 'ajax',
));
$this->setChild('generate', $button);
}
protected function _toHtml()
{
return $this->getChildHtml();
}
}
This doesn't cover the AJAX part, but will get you very close to getting the rest to work.
I found much information about pagination in Kohana 3.2 but most of it is scattered across forum comments and blog posts with no single complete source to refer to.
(note: I intended to self answer this question)
This is what worked for me:
Download the pagination module from https://github.com/kloopko/kohana-pagination (pagination was removed from Kohana 3.2, so this is an adapted module).
Install the module in modules/pagination.
Add the module in bootstrap.php:
Kohana::modules(array(
// ... other modules ...
'pagination' => MODPATH.'pagination'
));
Copy the configuration file from modules/pagination/config/pagination.php to application/config/pagination.php.
Add the following actions to your controller:
public function action_index() {
// Go to first page by default
$this->request->redirect('yourcontroller/page/?page=1');
}
public function action_page() {
$orm = orm::factory('your_orm');
$pagination = Pagination::factory(array(
'total_items' => $orm->count_all(),
'items_per_page' => 20,
)
);
// Pass controller and action names explicitly to $pagination object
$pagination->route_params(array('controller' => $this->request->controller(), 'action' => $this->request->action()));
// Get data
$data = $orm->offset($pagination->offset)->limit($pagination->items_per_page)->find_all()->as_array();
// Pass data and validation object to view
echo View::factory('yourview/page', array('data' => $data, 'pagination' => $pagination));
}
Create yourview/page as follows:
<?php
foreach($data as $item) {
// ...put code to list items here
}
// Show links
echo $pagination;
Modify application/config/pagination.php according to your needs. I had to change the 'view' parameter to 'pagination/floating' which displays ellipses (...) when the list of pages is too large, unlike the default 'pagination/basic' which lists all pages regardless of length.
Pagination wasn't originally working/supported in Kohana 3.2. Luckily, somebody has updated the module and you can get the code at https://github.com/kloopko/kohana-pagination