Drupal: How to show results in another page (PHP) - drupal

I need to know how to show results in another page in PHP. After much research in the files drupal site. The existence of a file "search-result.tpl.php"
So, how to show this in another page php?
search-result.tpl.php
<?php
/**
* #file
* Default theme implementation for displaying a single search result.
*
* This template renders a single search result and is collected into
* search-results.tpl.php. This and the parent template are
* dependent to one another sharing the markup for definition lists.
*
* Available variables:
* - $url: URL of the result.
* - $title: Title of the result.
* - $snippet: A small preview of the result. Does not apply to user searches.
* - $info: String of all the meta information ready for print. Does not apply
* to user searches.
* - $info_split: Contains same data as $info, split into a keyed array.
* - $module: The machine-readable name of the module (tab) being searched, such
* as "node" or "user".
* - $title_prefix (array): An array containing additional output populated by
* modules, intended to be displayed in front of the main title tag that
* appears in the template.
* - $title_suffix (array): An array containing additional output populated by
* modules, intended to be displayed after the main title tag that appears in
* the template.
*
* Default keys within $info_split:
* - $info_split['type']: Node type (or item type string supplied by module).
* - $info_split['user']: Author of the node linked to users profile. Depends
* on permission.
* - $info_split['date']: Last update of the node. Short formatted.
* - $info_split['comment']: Number of comments output as "% comments", %
* being the count. Depends on comment.module.
*
* Other variables:
* - $classes_array: Array of HTML class attribute values. It is flattened
* into a string within the variable $classes.
* - $title_attributes_array: Array of HTML attributes for the title. It is
* flattened into a string within the variable $title_attributes.
* - $content_attributes_array: Array of HTML attributes for the content. It is
* flattened into a string within the variable $content_attributes.
*
* Since $info_split is keyed, a direct print of the item is possible.
* This array does not apply to user searches so it is recommended to check
* for its existence before printing. The default keys of 'type', 'user' and
* 'date' always exist for node searches. Modules may provide other data.
* #code
* <?php if (isset($info_split['comment'])): ?>
* <span class="info-comment">
* <?php print $info_split['comment']; ?>
* </span>
* <?php endif; ?>
* #endcode
*
* To check for all available data within $info_split, use the code below.
* #code
* <?php print '<pre>'. check_plain(print_r($info_split, 1)) .'</pre>'; ?>
* #endcode
*
* #see template_preprocess()
* #see template_preprocess_search_result()
* #see template_process()
*/
?>
<li class="<?php print $classes; ?>"<?php print $attributes; ?>>
<?php print render($title_prefix); ?>
<h3 class="title"<?php print $title_attributes; ?>>
<?php print $title; ?>
</h3>
<?php print render($title_suffix); ?>
<div class="search-snippet-info">
<?php if ($snippet): ?>
<p class="search-snippet"<?php print $content_attributes; ?>><?php print $snippet; ?></p>
<?php endif; ?>
<?php if ($info): ?>
<p class="search-info"><?php print $info; ?></p>
<?php endif; ?>
</div>
</li>

What I believev you probably want to do is copy the search-result.tpl.php file to your themes directory and then modify it directly.
I do know that Drupal looks in your theme directory for template files first, which allows you to "override" core functionality without corrupting the system.
If you manage to fatally break the search-result.tpl.php file, you can just delete or rename it and get the original functionality back.

Related

Add product attribute to Woocommerce's blocks in Gutenberg

EDIT I: I have found the file where the old plugin Woocommerce Blocks sets the blocks: https://github.com/woocommerce/woocommerce-gutenberg-products-block/blob/master/src/BlockTypes/FeaturedCategory.php But where is it in the Woocommerce library?
EDIT II: Question in short:
How do you customize the Woocommerce Blocks to show more data than the build in functionality?
------------- background ------------
If you search for adding custom attributes for Woocommerce Blocks you find a lot of WordPress examples for this.
For example, this, where the answer points out, that you can add attributes by using the blocks.registerBlockType. But how to do this for Woocommerce Blocks?
I want to be able to add a data field to the HTML output. The data field should then call a product attribute and show if it exists.
So when you use the Woocommerce Blocks on your front page - for example, the size will be shown underneath the add to cart button - as in the image.
As you might know the functionality of showing/hiding the price, add-to-cart-button, reviews are already there, when you choose a Woocommerce Block on the editing site.
But I haven't found the place where this functionality is created.
This would also be a great help actually - if you could show me where in the Woocommerce Github library the blocks are being created. Maybe I can figure out my self how to filter through them and add the functionality
I know - based on a Udemy course - how to create a custom plugin and create a new blog-type, save and edit.
But I need to figure out what Woocommerce namespace is, how they create their blocks, and what their data is called. The Woocommerce developer handbook is not saying anything about this - not what I've found.
I've been searching the internet for three days now, and I just don't understand that I can't seem to find ANYTHING on this. That nobody else wants to customize this functionality in Woocommerce. I know it is a new function (blocks) in the core, but still.
I just need to be pointed in the right direction.
I was dealing with the exact same problem as you and I found the answer by digging deeply on the WC blocks plugin repo.
I found that you have to apply a filter to this hook: woocommerce_blocks_product_grid_item_html
The original HTML is this:
<li class="wc-block-grid__product">
<a href="{$data->permalink}" class="wc-block-grid__product-link">
{$data->image}
{$data->title}
</a>
{$data->badge}
{$data->price}
{$data->rating}
{$data->button}
</li>
So that way you can filter the html code and modify it by adding this chunk of code to your functions.php and customizing it to fit your needs
function wc_add_date_to_gutenberg_block( $html, $data, $product ) {
$dateStr = get_post_meta($product->get_id(), 'ticket_start_time', true);
$date = new DateTime($dateStr);
$data->date = "<p>Date: " . $date->format('d-m-Y H:i') . "</p>";
$output = "
<li class=\"wc-block-grid__product\">
<a href=\"{$data->permalink}\" class=\"wc-block-grid__product-link\">
{$data->image}
{$data->title}
</a>
{$data->date} <- I added this one
{$data->badge}
{$data->price}
{$data->rating}
{$data->button}
</li>
";
return $output;
}
add_filter("woocommerce_blocks_product_grid_item_html", "wc_add_date_to_gutenberg_block", 10, 3);
I'm not totally clear on what you're asking. You reference Gutenberg Blocks often, but have linked to a WooCommerce repository that doesn't have any Gutenberg Blocks.
But if I'm understanding you correctly, you're looking for the PHP template that controls products. You can find in content-product.php
You'll see a lot of calls to do_action which is core to WordPress hooks as used in plugin development.
<li <?php wc_product_class( '', $product ); ?>>
<?php
/**
* Hook: woocommerce_before_shop_loop_item.
*
* #hooked woocommerce_template_loop_product_link_open - 10
*/
do_action( 'woocommerce_before_shop_loop_item' );
/**
* Hook: woocommerce_before_shop_loop_item_title.
*
* #hooked woocommerce_show_product_loop_sale_flash - 10
* #hooked woocommerce_template_loop_product_thumbnail - 10
*/
do_action( 'woocommerce_before_shop_loop_item_title' );
/**
* Hook: woocommerce_shop_loop_item_title.
*
* #hooked woocommerce_template_loop_product_title - 10
*/
do_action( 'woocommerce_shop_loop_item_title' );
/**
* Hook: woocommerce_after_shop_loop_item_title.
*
* #hooked woocommerce_template_loop_rating - 5
* #hooked woocommerce_template_loop_price - 10
*/
do_action( 'woocommerce_after_shop_loop_item_title' );
/**
* Hook: woocommerce_after_shop_loop_item.
*
* #hooked woocommerce_template_loop_product_link_close - 5
* #hooked woocommerce_template_loop_add_to_cart - 10
*/
do_action( 'woocommerce_after_shop_loop_item' );
?>
</li>
If you do a search for the action hooks defined in content-product.php, you'll find them defined in wc-template-hooks.php. Hooks are named actions that functions are added to. For example if you look into the woocommerce_after_shop_loop_item action, you'll find these two functions being attached to it.
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
add_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_add_to_cart', 10 );
The woocommerce_template_loop_product_link_close and woocommerce_template_loop_add_to_cart functions are defined in wc-template-functions.php
You could create an entirely new content-product.php file in your theme by creating a file in yourtheme/woocommerce/content-product.php, however you then lose a lot of the built in power and compatibility of WooCommerce.
Better would be to remove then add new actions to the woocommerce_after_shop_loop_item hook. For example, woocommerce_template_loop_product_link_close is currently defined as:
function woocommerce_template_loop_product_link_close() {
echo '</a>';
}
You could overwrite this by doing this in your functions.php file:
remove_action( 'woocommerce_after_shop_loop_item', 'woocommerce_template_loop_product_link_close', 5 );
function custom_template_loop_product_link_close() {
echo 'Hello world!</a>';
}
add_action( 'woocommerce_after_shop_loop_item', 'custom_template_loop_product_link_close', 5 );
I hope this helps.

how to get the list of medias attached to a page?

I created a template (call it A.php) for a specific set of pages on my website.
How would one retrieve the list of medias (previously) attached to a page whose template is A.php in A.php?
I want to be able to iterate through that list of media in this A.php template. Here is a minimalistic version of A.php:
<?php
/**
* Template Name: test layout
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage jbWeb
* #since 1.0
* #version 1.0
*/
get_header();
query_posts(array('pagename' => get_query_var('pagename')));
while (have_posts()) {
the_post();
ob_start();
the_title();
$title = ob_get_clean();
echo $title;
$medias = get_attached_media( 'image', get_the_ID());
foreach($medias as $media) {
echo ('this is a url: ' . wp_get_attachment_url($media->id));
}
}
get_footer();
?>
apparently the only post that is retrieved from the while(have_posts()) loop is the page itself (or is it the content of the page itself?). This is what is shown on my browser:
The Url of any media attached to the page is never shown. And there are only two medias when I attached four photos to "pagetest1".
Here is what my admin page for the page "pagetest1" looks like:
Well here is a solution I have found which allows for any type of media to be displayed (you just have to tweak a part of the solution I will show how):
<?php
/**
* The template for displaying all pages
* Template Name: test layout
* #link https://codex.wordpress.org/Template_Hierarchy
*
* #package WordPress
* #subpackage jbWeb
* #since 1.0
* #version 1.0
*/
get_header();
//the query to select the post id of the post containing all content of this page.
//have to replace pageTest by get_query_var('pagename')
$page_content = $wpdb->get_var(
$wpdb->prepare("
select post_content from (select max(post_date) as post_date from wp_posts
where post_title='%s' group by post_title) as sub,
wp_posts as p where p.post_date = sub.post_date ;",
get_query_var('pagename'))
);
$matches = array();
preg_match_all('/<img.*\/>/mU', $page_content, $matches, PREG_PATTERN_ORDER);
foreach ($matches as $m) {
foreach ($m as $m1) {
echo $m1 . "\n";
}
}
get_footer();
?>
Explanation:
The mysql query
So first there is that query on the mysql database that is done where I select the content of the most recent version of the post intrinsic to the page whose template is "test layout". I pass get_query_var('pagename') as parameter to this mysql query, this get_query_var function returns the current page name.
the pattern match
Then I find all occurences of the <img/> tag within the content of that post. About the m and U option within the pattern: m signifies that I want the input $page_content to be seen as a single line. And U signifies that I want the regexp to not be greedy, so that I find all tags and not simply one tag that would contain other tags.
printing the result
Finally the double foreach loop prints all <img/> tag within that resulting page.
Note: You could find all occurences of a <video/> tag in the same way. Just replace <img.*\/> within the pattern by <video.*\/>

Drupal 8 - Accessing _entity in content variable in template to print out content

I am trying to print out a view, field by field so I can apply my style to it and customize the look of it.
When I copy over the views-view-unformatted--MY-TEMPLATE.html.twig
I can see these variables in the default view:
{#
/**
* #file
* Default theme implementation to display a view of unformatted rows.
*
* Available variables:
* - title: The title of this group of rows. May be empty.
* - rows: A list of the view's row items.
* - attributes: The row's HTML attributes.
* - content: The row's content.
* - view: The view object.
* - default_row_class: A flag indicating whether default classes should be
* used on rows.
*
* #see template_preprocess_views_view_unformatted()
*
* #ingroup themeable
*/
#}
After using {{ kint(content) }}, I can see that I have an
_entity field as show
However I can't figure out how to print out those specific fields, or even use Kint to find out how to separate them. I've tried doing something like so:
{{ kint(content._entity) }}
to no avail, and a tonne of other ways of trying to print it out.
Any help is appreciated!
It's probably because Content._entity is a node entity, not a renderable array that you can print.

Wordpress - List all pages that have children but show children than for the current page

The tree of my pages :
- Teeshirts
* Long
* Big
* Short
- Sweat
* Hot
* Cold
- Pants
- Skirts
On the active page (for example Teeshirts) , I want to display all parent pages and children of the current page :
- Teeshirts (ACTIVE PAGE)
* Long
* Big
* Short
- Sweat
- Pants
- Skirts
The children pages of sweat is not display.
Can you help me ?
Thank youuuu
try this
<?php
if($post->post_parent)
$children = wp_list_pages("title_li=&child_of=".$post->post_parent."&echo=0");
else
$children = wp_list_pages("title_li=&child_of=".$post->ID."&echo=0");
if ($children) {
$parent_title = get_the_title($post->post_parent);?>
<li><?php echo $parent_title;?></li>
<?php echo $children; ?>
<?php } ?>
Read also,
https://codex.wordpress.org/Function_Reference/wp_list_pages
https://wordpress.org/support/topic/wp_list_pages-parent-and-children-pages

Wordpress Pagination - loop posts

Is there a way that I can change my pagination code to find the next or previous post even if there isn't a next post? So if I was on my first post it says "prev" and i want it to say next too and to find the last one posted. So it loops the posts. Not sure if this is possible
<div class="nav">
<ul>
<li><?php previous_post_link('%link', 'previous', TRUE); ?></li>
<li><?php next_post_link('%link', 'next', TRUE); ?></li>
</ul>
</div>
Yes, it is possible, through filters.
/**
* Functon creates default content for pagination function previous_post_link() and next_post_link()
* #param $original string HTML content returned by previous_post_link/next_post_link to be modified
* #return string
*/
function default_prev_next_link($original) {
//if prev/next link does not exists, $original is empty string
if(!$original)
return "<a href='#'>new content</a>";
//you need to return original value, in case it is set
return $original;
}
//hook our function to filters
add_filter('previous_post_link', 'default_prev_next_link');
add_filter('next_post_link', 'default_prev_next_link');
Eventually, you can create two separate functions, which you hook into *_post link separately

Resources