drupal: add search field to an article - php

how can I add the search field (searching for all content and tags in my webiste) to my article ?
I've already installed and activated the module, but I would like to add it to a single article instead of a block of my website
thanks

There are several ways of doing searching in Drupal, but I guess you refer to the search module that's in Drupal core. It's usually used as a block which makes it a bit tricky if you want to display it with the article content.
You can use the theme function to theme a block like this:
<?php print theme('block' $block); >?
Where $block is a block object with all the data needed. To use this in your case, you could make a custom template for the article, since you only want to display the search block on one article. You would also need to make a preprocess function for the article (preprocess_node) where you create the block object. You could also just create some logic in the preprocess function and make a generic node template where you only print the block when it's there.
It is possible to control when blocks are displayed, so you could just setup your search block to only be displayed on your chosen article. That is far less work, but you wont be able to display it with the article content.

Related

What is the best way to create page in Drupal 8 so that I can then add these posts with images?

I have a Home page on the Drupal website (such as is created after installation), but I still need to create this page:
What is the best way to create a page so that I can then add these posts with images?
I am just starting to learn Drupal and have heard so far about such ways of creating pages:
1) in admin toolbar: Content / Add Content / Article
2) in admin toolbar: Content / Add Content / Basic page
3) in admin toolbar: Structure / Views / Add Views
Which one should I use? Or maybe there is some other option that I don’t know about?
P.S. At the moment I am more interested how to create empty page on which I can then add posts later, and adding posts it is another question.
Welcome to Drupal.
Drupal ships with the default theme which won't look nice but it does its job in the right way. Now if you want to create a better UI/UX obviously you should create a new theme. But before that make sure to read and understand the concepts behind Drupal. Drupal docs are your first friend.
Drupal Documentation
Drupal considers everything as nodes and that's how Drupal got its power. As you mentioned, Articles, Basic Page etc are called content types and they can be used to create a particular type of content.
Now for your purpose create a new content type and add the fields you need. From the image above I can say your content type needs Title, Image, Category and Date. After creating content type you can create as many contents as you want under the content type you just created. Consider each card in your image as content.
Now you can use a Drupal Core Module Views, to perform DataBase Operations without writing single code. Yes, you can select fields, sort, order etc with Views UI and display it in a page or a part of a page (Block).
I would say just try this out in the default Drupal theme and when you understand how this works, you can start creating your own theme for your project.
Theming Drupal
There is a lot of resources available. But you have to make sure what you are asking is whether you actually need. It will take some time, but it worth.
To build layouts for homepages on Drupal 8 you best friend is https://www.drupal.org/docs/8/core/modules/layout-builder
To build the content blocks inside your home page, you should start creating nodes on a node content type to hold your information. For instance: news content type, with a title, a body, a date, and an image.
For every node type, ex. news, work on the preview display, full display, and any other display which makes sense. These displays can be used later in the Layout Builder directly or in Views, referenced below.
If you wish your list to be dynamic, such as the last 10 entries are shown first, then use a view to hold the content sorted and filtered as you need.
In a nutshell.
Create a content type for your article/news.
Modify the displays of the content types to have at least a summary and full view.
Create the content itself to have something to see.
Create the view (block) to filter and sort your content.
Create a page layout (this makes sense for landing pages) which places your new view and any other content you need in any disposition.
This is roughly what I would do. The steps described above contain many intermediate steps. If in doubt, check the docs.
I hope that helps!
First, decide what will you display on that page. Is it content in some existing content type (article maybe) or you want to crate new one for this purpose.
If you need new one then create it (Structure -> Content types -> Add content type).
Then check what fields will you need. I.e. image, some description text. Add missing ones.
Create few nodes (pages) in that type so you could work with them.
Then for displaying you should crate a view (Structure -> Views -> Add view). It can be a page view (you are displaying only that content on page) or block view (this is just a block among some others). If you create a page you could visit it and if you create a block you have to add it to some region to appear on page (Structure -> Block Layout).
Inside your theme you should create templates for this page/block. Turn on twig debug mode so it will show you hints - what templates are used and how can you name yours to override default ones.
Adjust CSS to make it look like you want it to look.
Find some tutorial(s) for the details
Previous answers have given the flow of the work you should go through, I would like to add some resource that might help you achieve this.
Creating content type and fields: https://www.drupal.org/docs/administering-a-drupal-site/managing-content-0/working-with-content-types-and-fields
https://www.drupal.org/docs/user_guide/en/structure-content-type.html
View and View modes: https://www.drupal.org/docs/user_guide/en/views-concept.html
https://www.drupal.org/docs/8/api/entity-api/display-modes-view-modes-and-form-modes
Handling block of the view: https://www.drupal.org/docs/8/core/modules/block/overview
Feel free to ask if any further explanation is needed.
Thank you

Drupal 8 - Getting dynamic content with view data into a page/template

I'm new to Drupal and Drupal 8 and I want to add areas(blocks?) that I can easily add to a page that pulls in and has access to selected view data and fields. What's the easiest way to get this done? It seems like it should be something that's easy to do but Drupal seems to make this really hard to achieve.
I've tried making custom blocks and types via the CMS but then it'd mean editing the content in 2 different places when we need to update areas or add new areas.
I've also tried building a block via a module but I cant seem to pass the information from a view into my template that way.
The blocks will potentially need to be added to more than one page if that' possible too.
What am I missing, why is this so difficult?
Thanks
Although the question is not so specific I would recommend to start from here Understanding Drupal > Overview > The Drupal flow and then move to Drupal Glossary (it is almost the same for 8.x).
This way it will be more easy to understand Drupal parts such as Theme, Region, Block, Template (using Twig for 8.x) etc.
Use the views module to create the block, then use the block layout page admin/structure/block to place the block in an appropriate region. Edit the block and configure the block and set its visibility settings.

Magento discover if phtml is part of main product page or related product tab

I'm customizing my Magento webstore to use rich snippets, so that I had changed the price.phtml to add the itemprop="price" to the price span. The problemn is that I'm getting various itemprop attributes when I have related or agregate products.
I would like to know if there is a way to get the block parent name in php, so I'll be able to ignore the itemprop rendering out of the main product.
The price.phtml template and the corresponding block is almost always called via Mage_Catalog_Block_Product_Abstract::getPriceHtml($product, $displayMinimalPrice, $idSuffix), and therefore has no parent.
To detect from which blocks it is used, you could update those blocks to add something like $product->setIsCalledFromParentBlock(true) before the getPriceHtml() call, and then test the value of $product->getIsCalledFromParentBlock() in price.phtml
I would advise you to get a developer-toolbar extension like mgt-commerce is offering one for free! You get all kind of information straight in your webbrowser.
Good luck!

How do you create a "linked list" of Story nodes in Drupal 6?

I'm trying to link together a number of pages in a regular flow in Drupal. The way I've been doing it recently, is to create a new Menu and then seperately create a page for each menu item.
For example, I have a menu block with links One through to Six. I then create six pages and link One to node/1, Two to node/2, Three to node/3 and so on.
This is fine, but I'm told that I can create a number of story nodes instead, and they'll link together at the bottom of the page like so (http://imgur.com/xCtYe) without my intervention, almost like a linked list of page nodes.
How would I achieve this?
I worked it out. I had to enable the book module. Co-worker just explained to me after reading the question :)
You might also check out NodeQueue
I see you already solved it, but just FYI, you can also use the Custom Pagers module to do this as an alternative to the book module. Here is an excerpt from its project page:
... allows administrators to define context-sensitive previous/next pagers for any node type. The list of nodes to be cycled through is generated by a user-selectable view, and each pager can be displayed above or below the node body or in a sidebar block. This makes it possible to duplicate the paging functions of forum.module and book.module, as well as more complex stuff like comic strip navigation.

Drupal *_preprocess with a module

I'm new to Drupal dev, and was trying to add an existing region variable to my module's preprocessor function.
Basically, I've created a new region for my site (here's the definition from within side my theme's .info file):
regions[feeds] = Feeds
From Administer->Blocks, I've added the blocks I want to the new "Feeds" region.
Then, from another module, the "Advanced Front Page" module, I'm trying to add some PHP to my "front page" inside this module. The Advanced Front Page module just allows the site to have a landing page, instead of immediately viewing a list of other site content, etc. I've enabled PHP for the content area, and then added the following:
<div>
<?php print $feeds; ?>
</div>
It does not print the "Feeds" region, and I believe it's because that region variable is not accessible from outside of the page.tpl.php file. So after looking around, I came upon these links:
http://drupal.org/node/223430
http://drupal.org/node/237391
From there, I tried to add a preprocessor function for the module "Advanced Front Page", which has a module name of "front_page" (or possibly just "front", I'm not 100% sure). Here's my preprocessor function, that I tried to add to both my template.php file, and the /modules/front/front_page.module file (not at the same time, mind you):
function front_preprocess(&$vars)
{
$vars['feeds'] = theme('blocks', 'feeds');
}
Regardless of where I've placed this file (template.php or front_page.module) it doesn't seem to do anything. Any idea where I might be going wrong?
There are several points to address in your question:
I'd second googletorps answer in that you should approach this in a different way.
The *_preprocess functions can only be used to manipulate/inject variables for templates or theme functions, e.g. page.tpl.php, node.tpl.php, theme_links(), etc. As the front_page module does not use a theme function or (special) template to render its output, you can not make the $feed variable there by means of a *_preprocess function.
Sidenote: With *_preprocess functions, naming is crucial. You need to prefix the function name either with the exact module name or the theme name, depending on where you declare it. So in your example, if you want to add a preprocess function to the module, you'd prefix it with 'front_page_', if you add it to your themes template.php, you'd add 'yourThemeName_'.
You could achieve what you want by creating the blocks directly from code in your frontpage content area. Instead of trying to output the (not available) $feed variable, you could call:
theme('blocks', 'feeds')
This will cause Drupal to return the themed blocks for the given region ('feeds' in this case). Note that this is still not a good way to do it, as even if you don't use the region in your page.tpl.php, it still gets created for every page request made to your site!
So I would go with googletorps suggestion, adding the new region only if there are other uses for it also. If I'd just wanted to add some blocks to the frontpage, I would not create a new region, but configure the blocks to show in the content region and simply restrict them to show only for <front> in their visibility settings.
I haven't tried the advanced front page module, but when dealing with regions, you shouldn't do what you have done. It's a bit hacky and actually not needed. I don't know how the module hook into the templating system, but your problem is probably getting region variables into it's scope. But instead of trying to get the region into the frontpage using the module, you should instead get it into your page.tpl.php. You can actually do what you first tried, but I would suggest that you alter it a bit like this:
<?php if ($feeds): ?>
<div id="feeds">
<?php print $feeds; ?>
</div>
<?php endif; ?>
I have improved in two ways.
By adding the if statement, you don't add empty markup. So you wont get an empty div if $feeds doesn't contain anything.
Adding id's to regions is a good idea. It makes styling them or their content a lot easier, also it adds semantic to your page which html is all about.
Now if you only want your blocks to be shown in the front page you can set that up in each of the blocks settings. So you could possibly just use a region that already exist, unless you want your blocks display a outside an existing region. When adding regions it's not a good practice to only add a region to a single page, instead it's much better to control when it's content should be shown. It might be that you don't need to create a new region, but simply can use one that's already made. Also if you want to make some template changes to your front page, you can also create the front-page.tpl.php where you can create a different template layout for your front page if you so desire.

Categories