Adding icons to wordpress navigation menu - php

I want to add an icon after <a> which is in <li>.
I modified the file class-walker-nav-menu.php, added something like this, but the if condition doesn't work, without it, icons are added but to each element, and I want them to be added only to <li> that have children.
$item_output = $args->before;
$item_output .= $menu_item->has_children ? '<i class="icon1"></i>' : '';
$item_output .= '<a' . $attributes . '>';
$item_output .= $args->link_before . $title . $args->link_after;
$item_output .= '</a>';
$item_output .= $menu_item->has_children ? '<i class="icon2"></i>' : '';
$item_output .= $args->after;
And here is all code /
Thank you for your help!
<?php
/**
* Nav Menu API: Walker_Nav_Menu class
*
* #package WordPress
* #subpackage Nav_Menus
* #since 4.6.0
*/
/**
* Core class used to implement an HTML list of nav menu items.
*
* #since 3.0.0
*
* #see Walker
*/
class Walker_Nav_Menu extends Walker {
/**
* What the class handles.
*
* #since 3.0.0
* #var string
*
* #see Walker::$tree_type
*/
public $tree_type = array( 'post_type', 'taxonomy', 'custom' );
/**
* Database fields to use.
*
* #since 3.0.0
* #todo Decouple this.
* #var string[]
*
* #see Walker::$db_fields
*/
public $db_fields = array(
'parent' => 'menu_item_parent',
'id' => 'db_id',
);
/**
* Starts the list before the elements are added.
*
* #since 3.0.0
*
* #see Walker::start_lvl()
*
* #param string $output Used to append additional content (passed by reference).
* #param int $depth Depth of menu item. Used for padding.
* #param stdClass $args An object of wp_nav_menu() arguments.
*/
public function start_lvl( &$output, $depth = 0, $args = null ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = str_repeat( $t, $depth );
// Default class.
$classes = array( 'sub-menu' );
// Add custom class to sub-menu.
$classes[] = 'my-custom-class';
/**
* Filters the CSS class(es) applied to a menu list element.
*
* #since 4.8.0
*
* #param string[] $classes Array of the CSS classes that are applied to the menu `<ul>` element.
* #param stdClass $args An object of `wp_nav_menu()` arguments.
* #param int $depth Depth of menu item. Used for padding.
*/
$class_names = implode( ' ', apply_filters( 'nav_menu_submenu_css_class', $classes, $args, $depth ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$output .= "{$n}{$indent}<ul$class_names>{$n}";
}
/**
* Ends the list of after the elements are added.
*
* #since 3.0.0
*
* #see Walker::end_lvl()
*
* #param string $output Used to append additional content (passed by reference).
* #param int $depth Depth of menu item. Used for padding.
* #param stdClass $args An object of wp_nav_menu() arguments.
*/
public function end_lvl( &$output, $depth = 0, $args = null ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = str_repeat( $t, $depth );
$output .= "$indent</ul>{$n}";
}
/**
* Starts the element output.
*
* #since 3.0.0
* #since 4.4.0 The {#see 'nav_menu_item_args'} filter was added.
* #since 5.9.0 Renamed `$item` to `$data_object` and `$id` to `$current_object_id`
* to match parent class for PHP 8 named parameter support.
*
* #see Walker::start_el()
*
* #param string $output Used to append additional content (passed by reference).
* #param WP_Post $data_object Menu item data object.
* #param int $depth Depth of menu item. Used for padding.
* #param stdClass $args An object of wp_nav_menu() arguments.
* #param int $current_object_id Optional. ID of the current menu item. Default 0.
*/
public function start_el( &$output, $data_object, $depth = 0, $args = null, $current_object_id = 0 ) {
// Restores the more descriptive, specific name for use within this method.
$menu_item = $data_object;
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$indent = ( $depth ) ? str_repeat( $t, $depth ) : '';
$classes = empty( $menu_item->classes ) ? array() : (array) $menu_item->classes;
$classes[] = 'menu-item-' . $menu_item->ID;
/**
* Filters the arguments for a single nav menu item.
*
* #since 4.4.0
*
* #param stdClass $args An object of wp_nav_menu() arguments.
* #param WP_Post $menu_item Menu item data object.
* #param int $depth Depth of menu item. Used for padding.
*/
$args = apply_filters( 'nav_menu_item_args', $args, $menu_item, $depth );
/**
* Filters the CSS classes applied to a menu item's list item element.
*
* #since 3.0.0
* #since 4.1.0 The `$depth` parameter was added.
*
* #param string[] $classes Array of the CSS classes that are applied to the menu item's `<li>` element.
* #param WP_Post $menu_item The current menu item object.
* #param stdClass $args An object of wp_nav_menu() arguments.
* #param int $depth Depth of menu item. Used for padding.
*/
$class_names = implode( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $menu_item, $args, $depth ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
/**
* Filters the ID attribute applied to a menu item's list item element.
*
* #since 3.0.1
* #since 4.1.0 The `$depth` parameter was added.
*
* #param string $menu_item_id The ID attribute applied to the menu item's `<li>` element.
* #param WP_Post $menu_item The current menu item.
* #param stdClass $args An object of wp_nav_menu() arguments.
* #param int $depth Depth of menu item. Used for padding.
*/
$id = apply_filters( 'nav_menu_item_id', 'menu-item-' . $menu_item->ID, $menu_item, $args, $depth );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
$output .= $indent . '<li' . $id . $class_names . '>';
$atts = array();
$atts['title'] = ! empty( $menu_item->attr_title ) ? $menu_item->attr_title : '';
$atts['target'] = ! empty( $menu_item->target ) ? $menu_item->target : '';
if ( '_blank' === $menu_item->target && empty( $menu_item->xfn ) ) {
$atts['rel'] = 'noopener';
} else {
$atts['rel'] = $menu_item->xfn;
}
$atts['href'] = ! empty( $menu_item->url ) ? $menu_item->url : '';
$atts['aria-current'] = $menu_item->current ? 'page' : '';
/**
* Filters the HTML attributes applied to a menu item's anchor element.
*
* #since 3.6.0
* #since 4.1.0 The `$depth` parameter was added.
*
* #param array $atts {
* The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
*
* #type string $title Title attribute.
* #type string $target Target attribute.
* #type string $rel The rel attribute.
* #type string $href The href attribute.
* #type string $aria-current The aria-current attribute.
* }
* #param WP_Post $menu_item The current menu item object.
* #param stdClass $args An object of wp_nav_menu() arguments.
* #param int $depth Depth of menu item. Used for padding.
*/
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $menu_item, $args, $depth );
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
/** This filter is documented in wp-includes/post-template.php */
$title = apply_filters( 'the_title', $menu_item->title, $menu_item->ID );
/**
* Filters a menu item's title.
*
* #since 4.4.0
*
* #param string $title The menu item's title.
* #param WP_Post $menu_item The current menu item object.
* #param stdClass $args An object of wp_nav_menu() arguments.
* #param int $depth Depth of menu item. Used for padding.
*/
$title = apply_filters( 'nav_menu_item_title', $title, $menu_item, $args, $depth );
$item_output = $args->before;
$item_output .= $menu_item->has_children ? '<i class="icon1"></i>' : '';
$item_output .= '<a' . $attributes . '>';
$item_output .= $args->link_before . $title . $args->link_after;
$item_output .= '</a>';
$item_output .= $menu_item->has_children ? '<i class="icon2"></i>' : '';
$item_output .= $args->after;
/**
* Filters a menu item's starting output.
*
* The menu item's starting output only includes `$args->before`, the opening `<a>`,
* the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
* no filter for modifying the opening and closing `<li>` for a menu item.
*
* #since 3.0.0
*
* #param string $item_output The menu item's starting HTML output.
* #param WP_Post $menu_item Menu item data object.
* #param int $depth Depth of menu item. Used for padding.
* #param stdClass $args An object of wp_nav_menu() arguments.
*/
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $menu_item, $depth, $args );
}
/**
* Ends the element output, if needed.
*
* #since 3.0.0
* #since 5.9.0 Renamed `$item` to `$data_object` to match parent class for PHP 8 named parameter support.
*
* #see Walker::end_el()
*
* #param string $output Used to append additional content (passed by reference).
* #param WP_Post $data_object Menu item data object. Not used.
* #param int $depth Depth of page. Not Used.
* #param stdClass $args An object of wp_nav_menu() arguments.
*/
public function end_el( &$output, $data_object, $depth = 0, $args = null ) {
if ( isset( $args->item_spacing ) && 'discard' === $args->item_spacing ) {
$t = '';
$n = '';
} else {
$t = "\t";
$n = "\n";
}
$output .= "</li>{$n}";
}
}

Related

How to add image parameter in Gloudemans Shoppingcart

I am trying to add an image parameter in Gloudemans Shoppingcart but am getting an error
Please supply a valid weight
I am pretty sure this is because am inserting the image parameter in a position configured for "weight", now my question is where do I add the image parameter? it doesn't seem to exist in any of the Gloudemans config files, please assist, below am listing all files under config in Gloudemans shoppingcart
Cart Controller
public function addItem($id)
{
$product=Product::find($id);
Cart::add($id,$product->product_name,1,$product->price,$product->product_image);
return back();
}
Cart.php file under folders vender\bumbummen99\src
<?php
namespace Gloudemans\Shoppingcart;
use Carbon\Carbon;
use Closure;
use Gloudemans\Shoppingcart\Contracts\Buyable;
use Gloudemans\Shoppingcart\Contracts\InstanceIdentifier;
use Gloudemans\Shoppingcart\Exceptions\CartAlreadyStoredException;
use Gloudemans\Shoppingcart\Exceptions\InvalidRowIDException;
use Gloudemans\Shoppingcart\Exceptions\UnknownModelException;
use Illuminate\Contracts\Events\Dispatcher;
use Illuminate\Database\DatabaseManager;
use Illuminate\Session\SessionManager;
use Illuminate\Support\Collection;
class Cart
{
const DEFAULT_INSTANCE = 'default';
/**
* Instance of the session manager.
*
* #var \Illuminate\Session\SessionManager
*/
private $session;
/**
* Instance of the event dispatcher.
*
* #var \Illuminate\Contracts\Events\Dispatcher
*/
private $events;
/**
* Holds the current cart instance.
*
* #var string
*/
private $instance;
/**
* Holds the creation date of the cart.
*
* #var mixed
*/
private $createdAt;
/**
* Holds the update date of the cart.
*
* #var mixed
*/
private $updatedAt;
/**
* Defines the discount percentage.
*
* #var float
*/
private $discount = 0;
/**
* Defines the tax rate.
*
* #var float
*/
private $taxRate = 0;
/**
* Cart constructor.
*
* #param \Illuminate\Session\SessionManager $session
* #param \Illuminate\Contracts\Events\Dispatcher $events
*/
public function __construct(SessionManager $session, Dispatcher $events)
{
$this->session = $session;
$this->events = $events;
$this->taxRate = config('cart.tax');
$this->instance(self::DEFAULT_INSTANCE);
}
/**
* Set the current cart instance.
*
* #param string|null $instance
*
* #return \Gloudemans\Shoppingcart\Cart
*/
public function instance($instance = null)
{
$instance = $instance ?: self::DEFAULT_INSTANCE;
if ($instance instanceof InstanceIdentifier) {
$this->discount = $instance->getInstanceGlobalDiscount();
$instance = $instance->getInstanceIdentifier();
}
$this->instance = sprintf('%s.%s', 'cart', $instance);
return $this;
}
/**
* Get the current cart instance.
*
* #return string
*/
public function currentInstance()
{
return str_replace('cart.', '', $this->instance);
}
/**
* Add an item to the cart.
*
* #param mixed $id
* #param mixed $name
* #param int|float $qty
* #param float $price
* #param float $weight
* #param array $options
*
* #return \Gloudemans\Shoppingcart\CartItem
*/
public function add($id, $name = null, $qty = null, $price = null, $weight = 0, array $options = [])
{
if ($this->isMulti($id)) {
return array_map(function ($item) {
return $this->add($item);
}, $id);
}
$cartItem = $this->createCartItem($id, $name, $qty, $price, $weight, $options);
return $this->addCartItem($cartItem);
}
/**
* Add an item to the cart.
*
* #param \Gloudemans\Shoppingcart\CartItem $item Item to add to the Cart
* #param bool $keepDiscount Keep the discount rate of the Item
* #param bool $keepTax Keep the Tax rate of the Item
* #param bool $dispatchEvent
*
* #return \Gloudemans\Shoppingcart\CartItem The CartItem
*/
public function addCartItem($item, $keepDiscount = false, $keepTax = false, $dispatchEvent = true)
{
if (!$keepDiscount) {
$item->setDiscountRate($this->discount);
}
if (!$keepTax) {
$item->setTaxRate($this->taxRate);
}
$content = $this->getContent();
if ($content->has($item->rowId)) {
$item->qty += $content->get($item->rowId)->qty;
}
$content->put($item->rowId, $item);
if ($dispatchEvent) {
$this->events->dispatch('cart.added', $item);
}
$this->session->put($this->instance, $content);
return $item;
}
/**
* Update the cart item with the given rowId.
*
* #param string $rowId
* #param mixed $qty
*
* #return \Gloudemans\Shoppingcart\CartItem
*/
public function update($rowId, $qty)
{
$cartItem = $this->get($rowId);
if ($qty instanceof Buyable) {
$cartItem->updateFromBuyable($qty);
} elseif (is_array($qty)) {
$cartItem->updateFromArray($qty);
} else {
$cartItem->qty = $qty;
}
$content = $this->getContent();
if ($rowId !== $cartItem->rowId) {
$itemOldIndex = $content->keys()->search($rowId);
$content->pull($rowId);
if ($content->has($cartItem->rowId)) {
$existingCartItem = $this->get($cartItem->rowId);
$cartItem->setQuantity($existingCartItem->qty + $cartItem->qty);
}
}
if ($cartItem->qty <= 0) {
$this->remove($cartItem->rowId);
return;
} else {
if (isset($itemOldIndex)) {
$content = $content->slice(0, $itemOldIndex)
->merge([$cartItem->rowId => $cartItem])
->merge($content->slice($itemOldIndex));
} else {
$content->put($cartItem->rowId, $cartItem);
}
}
$this->events->dispatch('cart.updated', $cartItem);
$this->session->put($this->instance, $content);
return $cartItem;
}
/**
* Remove the cart item with the given rowId from the cart.
*
* #param string $rowId
*
* #return void
*/
public function remove($rowId)
{
$cartItem = $this->get($rowId);
$content = $this->getContent();
$content->pull($cartItem->rowId);
$this->events->dispatch('cart.removed', $cartItem);
$this->session->put($this->instance, $content);
}
/**
* Get a cart item from the cart by its rowId.
*
* #param string $rowId
*
* #return \Gloudemans\Shoppingcart\CartItem
*/
public function get($rowId)
{
$content = $this->getContent();
if (!$content->has($rowId)) {
throw new InvalidRowIDException("The cart does not contain rowId {$rowId}.");
}
return $content->get($rowId);
}
/**
* Destroy the current cart instance.
*
* #return void
*/
public function destroy()
{
$this->session->remove($this->instance);
}
/**
* Get the content of the cart.
*
* #return \Illuminate\Support\Collection
*/
public function content()
{
if (is_null($this->session->get($this->instance))) {
return new Collection([]);
}
return $this->session->get($this->instance);
}
/**
* Get the number of items in the cart.
*
* #return int|float
*/
public function count()
{
return $this->getContent()->sum('qty');
}
/**
* Get the number of items instances in the cart.
*
* #return int|float
*/
public function countInstances()
{
return $this->getContent()->count();
}
/**
* Get the total price of the items in the cart.
*
* #return float
*/
public function totalFloat()
{
return $this->getContent()->reduce(function ($total, CartItem $cartItem) {
return $total + $cartItem->total;
}, 0);
}
/**
* Get the total price of the items in the cart as formatted string.
*
* #param int $decimals
* #param string $decimalPoint
* #param string $thousandSeperator
*
* #return string
*/
public function total($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return $this->numberFormat($this->totalFloat(), $decimals, $decimalPoint, $thousandSeperator);
}
/**
* Get the total tax of the items in the cart.
*
* #return float
*/
public function taxFloat()
{
return $this->getContent()->reduce(function ($tax, CartItem $cartItem) {
return $tax + $cartItem->taxTotal;
}, 0);
}
/**
* Get the total tax of the items in the cart as formatted string.
*
* #param int $decimals
* #param string $decimalPoint
* #param string $thousandSeperator
*
* #return string
*/
public function tax($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return $this->numberFormat($this->taxFloat(), $decimals, $decimalPoint, $thousandSeperator);
}
/**
* Get the subtotal (total - tax) of the items in the cart.
*
* #return float
*/
public function subtotalFloat()
{
return $this->getContent()->reduce(function ($subTotal, CartItem $cartItem) {
return $subTotal + $cartItem->subtotal;
}, 0);
}
/**
* Get the subtotal (total - tax) of the items in the cart as formatted string.
*
* #param int $decimals
* #param string $decimalPoint
* #param string $thousandSeperator
*
* #return string
*/
public function subtotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return $this->numberFormat($this->subtotalFloat(), $decimals, $decimalPoint, $thousandSeperator);
}
/**
* Get the discount of the items in the cart.
*
* #return float
*/
public function discountFloat()
{
return $this->getContent()->reduce(function ($discount, CartItem $cartItem) {
return $discount + $cartItem->discountTotal;
}, 0);
}
/**
* Get the discount of the items in the cart as formatted string.
*
* #param int $decimals
* #param string $decimalPoint
* #param string $thousandSeperator
*
* #return string
*/
public function discount($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return $this->numberFormat($this->discountFloat(), $decimals, $decimalPoint, $thousandSeperator);
}
/**
* Get the price of the items in the cart (not rounded).
*
* #return float
*/
public function initialFloat()
{
return $this->getContent()->reduce(function ($initial, CartItem $cartItem) {
return $initial + ($cartItem->qty * $cartItem->price);
}, 0);
}
/**
* Get the price of the items in the cart as formatted string.
*
* #param int $decimals
* #param string $decimalPoint
* #param string $thousandSeperator
*
* #return string
*/
public function initial($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return $this->numberFormat($this->initialFloat(), $decimals, $decimalPoint, $thousandSeperator);
}
/**
* Get the price of the items in the cart (previously rounded).
*
* #return float
*/
public function priceTotalFloat()
{
return $this->getContent()->reduce(function ($initial, CartItem $cartItem) {
return $initial + $cartItem->priceTotal;
}, 0);
}
/**
* Get the price of the items in the cart as formatted string.
*
* #param int $decimals
* #param string $decimalPoint
* #param string $thousandSeperator
*
* #return string
*/
public function priceTotal($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return $this->numberFormat($this->priceTotalFloat(), $decimals, $decimalPoint, $thousandSeperator);
}
/**
* Get the total weight of the items in the cart.
*
* #return float
*/
public function weightFloat()
{
return $this->getContent()->reduce(function ($total, CartItem $cartItem) {
return $total + ($cartItem->qty * $cartItem->weight);
}, 0);
}
/**
* Get the total weight of the items in the cart.
*
* #param int $decimals
* #param string $decimalPoint
* #param string $thousandSeperator
*
* #return string
*/
public function weight($decimals = null, $decimalPoint = null, $thousandSeperator = null)
{
return $this->numberFormat($this->weightFloat(), $decimals, $decimalPoint, $thousandSeperator);
}
/**
* Search the cart content for a cart item matching the given search closure.
*
* #param \Closure $search
*
* #return \Illuminate\Support\Collection
*/
public function search(Closure $search)
{
return $this->getContent()->filter($search);
}
/**
* Associate the cart item with the given rowId with the given model.
*
* #param string $rowId
* #param mixed $model
*
* #return void
*/
public function associate($rowId, $model)
{
if (is_string($model) && !class_exists($model)) {
throw new UnknownModelException("The supplied model {$model} does not exist.");
}
$cartItem = $this->get($rowId);
$cartItem->associate($model);
$content = $this->getContent();
$content->put($cartItem->rowId, $cartItem);
$this->session->put($this->instance, $content);
}
/**
* Set the tax rate for the cart item with the given rowId.
*
* #param string $rowId
* #param int|float $taxRate
*
* #return void
*/
public function setTax($rowId, $taxRate)
{
$cartItem = $this->get($rowId);
$cartItem->setTaxRate($taxRate);
$content = $this->getContent();
$content->put($cartItem->rowId, $cartItem);
$this->session->put($this->instance, $content);
}
/**
* Set the global tax rate for the cart.
* This will set the tax rate for all items.
*
* #param float $discount
*/
public function setGlobalTax($taxRate)
{
$this->taxRate = $taxRate;
$content = $this->getContent();
if ($content && $content->count()) {
$content->each(function ($item, $key) {
$item->setTaxRate($this->taxRate);
});
}
}
/**
* Set the discount rate for the cart item with the given rowId.
*
* #param string $rowId
* #param int|float $taxRate
*
* #return void
*/
public function setDiscount($rowId, $discount)
{
$cartItem = $this->get($rowId);
$cartItem->setDiscountRate($discount);
$content = $this->getContent();
$content->put($cartItem->rowId, $cartItem);
$this->session->put($this->instance, $content);
}
/**
* Set the global discount percentage for the cart.
* This will set the discount for all cart items.
*
* #param float $discount
*
* #return void
*/
public function setGlobalDiscount($discount)
{
$this->discount = $discount;
$content = $this->getContent();
if ($content && $content->count()) {
$content->each(function ($item, $key) {
$item->setDiscountRate($this->discount);
});
}
}
/**
* Store an the current instance of the cart.
*
* #param mixed $identifier
*
* #return void
*/
public function store($identifier)
{
$content = $this->getContent();
if ($identifier instanceof InstanceIdentifier) {
$identifier = $identifier->getInstanceIdentifier();
}
if ($this->storedCartWithIdentifierExists($identifier)) {
throw new CartAlreadyStoredException("A cart with identifier {$identifier} was already stored.");
}
$this->getConnection()->table($this->getTableName())->insert([
'identifier' => $identifier,
'instance' => $this->currentInstance(),
'content' => serialize($content),
'created_at' => $this->createdAt ?: Carbon::now(),
'updated_at' => Carbon::now(),
]);
$this->events->dispatch('cart.stored');
}
/**
* Restore the cart with the given identifier.
*
* #param mixed $identifier
*
* #return void
*/
public function restore($identifier)
{
if ($identifier instanceof InstanceIdentifier) {
$identifier = $identifier->getInstanceIdentifier();
}
if (!$this->storedCartWithIdentifierExists($identifier)) {
return;
}
$stored = $this->getConnection()->table($this->getTableName())
->where('identifier', $identifier)->first();
$storedContent = unserialize(data_get($stored, 'content'));
$currentInstance = $this->currentInstance();
$this->instance(data_get($stored, 'instance'));
$content = $this->getContent();
foreach ($storedContent as $cartItem) {
$content->put($cartItem->rowId, $cartItem);
}
$this->events->dispatch('cart.restored');
$this->session->put($this->instance, $content);
$this->instance($currentInstance);
$this->createdAt = Carbon::parse(data_get($stored, 'created_at'));
$this->updatedAt = Carbon::parse(data_get($stored, 'updated_at'));
$this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->delete();
}
/**
* Erase the cart with the given identifier.
*
* #param mixed $identifier
*
* #return void
*/
public function erase($identifier)
{
if ($identifier instanceof InstanceIdentifier) {
$identifier = $identifier->getInstanceIdentifier();
}
if (!$this->storedCartWithIdentifierExists($identifier)) {
return;
}
$this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->delete();
$this->events->dispatch('cart.erased');
}
/**
* Merges the contents of another cart into this cart.
*
* #param mixed $identifier Identifier of the Cart to merge with.
* #param bool $keepDiscount Keep the discount of the CartItems.
* #param bool $keepTax Keep the tax of the CartItems.
* #param bool $dispatchAdd Flag to dispatch the add events.
*
* #return bool
*/
public function merge($identifier, $keepDiscount = false, $keepTax = false, $dispatchAdd = true)
{
if (!$this->storedCartWithIdentifierExists($identifier)) {
return false;
}
$stored = $this->getConnection()->table($this->getTableName())
->where('identifier', $identifier)->first();
$storedContent = unserialize($stored->content);
foreach ($storedContent as $cartItem) {
$this->addCartItem($cartItem, $keepDiscount, $keepTax, $dispatchAdd);
}
$this->events->dispatch('cart.merged');
return true;
}
/**
* Magic method to make accessing the total, tax and subtotal properties possible.
*
* #param string $attribute
*
* #return float|null
*/
public function __get($attribute)
{
switch ($attribute) {
case 'total':
return $this->total();
case 'tax':
return $this->tax();
case 'subtotal':
return $this->subtotal();
default:
return;
}
}
/**
* Get the carts content, if there is no cart content set yet, return a new empty Collection.
*
* #return \Illuminate\Support\Collection
*/
protected function getContent()
{
if ($this->session->has($this->instance)) {
return $this->session->get($this->instance);
}
return new Collection();
}
/**
* Create a new CartItem from the supplied attributes.
*
* #param mixed $id
* #param mixed $name
* #param int|float $qty
* #param float $price
* #param float $weight
* #param array $options
*
* #return \Gloudemans\Shoppingcart\CartItem
*/
private function createCartItem($id, $name, $qty, $price, $weight, array $options)
{
if ($id instanceof Buyable) {
$cartItem = CartItem::fromBuyable($id, $qty ?: []);
$cartItem->setQuantity($name ?: 1);
$cartItem->associate($id);
} elseif (is_array($id)) {
$cartItem = CartItem::fromArray($id);
$cartItem->setQuantity($id['qty']);
} else {
$cartItem = CartItem::fromAttributes($id, $name, $price, $weight, $options);
$cartItem->setQuantity($qty);
}
return $cartItem;
}
/**
* Check if the item is a multidimensional array or an array of Buyables.
*
* #param mixed $item
*
* #return bool
*/
private function isMulti($item)
{
if (!is_array($item)) {
return false;
}
return is_array(head($item)) || head($item) instanceof Buyable;
}
/**
* #param $identifier
*
* #return bool
*/
private function storedCartWithIdentifierExists($identifier)
{
return $this->getConnection()->table($this->getTableName())->where('identifier', $identifier)->exists();
}
/**
* Get the database connection.
*
* #return \Illuminate\Database\Connection
*/
private function getConnection()
{
return app(DatabaseManager::class)->connection($this->getConnectionName());
}
/**
* Get the database table name.
*
* #return string
*/
private function getTableName()
{
return config('cart.database.table', 'shoppingcart');
}
/**
* Get the database connection name.
*
* #return string
*/
private function getConnectionName()
{
$connection = config('cart.database.connection');
return is_null($connection) ? config('database.default') : $connection;
}
/**
* Get the Formatted number.
*
* #param $value
* #param $decimals
* #param $decimalPoint
* #param $thousandSeperator
*
* #return string
*/
private function numberFormat($value, $decimals, $decimalPoint, $thousandSeperator)
{
if (is_null($decimals)) {
$decimals = config('cart.format.decimals', 2);
}
if (is_null($decimalPoint)) {
$decimalPoint = config('cart.format.decimal_point', '.');
}
if (is_null($thousandSeperator)) {
$thousandSeperator = config('cart.format.thousand_separator', ',');
}
return number_format($value, $decimals, $decimalPoint, $thousandSeperator);
}
/**
* Get the creation date of the cart (db context).
*
* #return \Carbon\Carbon|null
*/
public function createdAt()
{
return $this->createdAt;
}
/**
* Get the lats update date of the cart (db context).
*
* #return \Carbon\Carbon|null
*/
public function updatedAt()
{
return $this->updatedAt;
}
}
I have tried checcking out Crinsane's repo but there is no documentation about this

Unable to recreate SQL query with Doctrine

I am having issues trying to recreate the following query:
select *
from acme_opties_relaties as r
inner join acme_options as o on o.id = r.id_a
inner join acme_option_subgroup as osg on osg.option_id = o.id
where osg.subgroup_id in (309, 303, 306, 300)
and o.id <> 47086
and r.soort = 2
and o.project_id = 140018
What i get is the following query:
SELECT t0_.id_a AS id_a_17, t0_.id_b AS id_b_18,
t0_.soort AS soort_0, t1_.id AS id_1, t1_.external_id AS external_id_2, t1_.external_ref AS external_ref_3, t1_.`name`
AS name_4, t1_.name_sales AS name_sales_5, t1_.price AS price_6, t1_.warranty AS warranty_7, t1_.material AS material_8, t1_.no_option
AS no_option_9, t1_.print AS print_10, t1_.text_warranty AS text_warranty_11, t1_.text_technical AS text_technical_12, t1_.text_sales AS text_sales_13, t1_.`group`
AS group_14, t1_.created AS created_15, t1_.updated AS updated_16, t1_.project_id AS project_id_19
FROM acme_opties_relaties t0_
INNER JOIN acme_options t1_ ON
(t1_.id <> 47086 AND t0_.soort = 2 AND EXISTS
(SELECT 1 FROM acme_option_subgroup t2_ INNER JOIN acme_subgroep t3_ ON t2_.subgroup_id = t3_.id WHERE t2_.option_id = t1_.id AND t3_.id IN (309, 303, 306, 300))
AND t1_.project_id = 140018)
any pointers / directions would be appreciated.
<?php
declare(strict_types = 1);
namespace Acme\Domain\Entity\Option;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
use Acme\Domain\Entity\Element\SubElement;
use Acme\Domain\Value\Option\Relation\Type;
/**
* Class RelationRepository
* Relations are interpreted as following.
* id_a is depending on id_b
* id_a is selected by id_b
* id_a is excluded by id_b
*
* #package Acme\Domain\Entity\Option
*/
class RelationRepository extends EntityRepository
{
/**
* #var array
*/
private $directions = ['left', 'right'];
/**
* getLeft returns the left side of the relations.
*
* #param Option $option
* #param SubElement $subElement
* #param Type $type
* #return array
*/
private function getLeft(Option $option, SubElement $subElement, Type $type, bool $inversed = false): Query
{
return $this->getQuery($option, $subElement, $type, 'left', $inversed);
}
/**
* getRight returns the right side of the relations.
*
* #param Option $option
* #param SubElement $subElement
* #param Type $type
* #return array
*/
private function getRight(Option $option, SubElement $subElement, Type $type, bool $inversed = false): Query
{
return $this->getQuery($option, $subElement, $type, 'right', $inversed);
}
/**
* getQuery returns the full Query needed to get a Collection of Relations.
*
* #param Option $option
* #param SubElement $subElement
* #param Type $type
* #param string $direction
* #return Query
*/
private function getQuery(Option $option, SubElement $subElement, Type $type, string $direction, bool $inversed): Query
{
$direction = strtolower($direction);
$this->validateDirection($direction);
$qb = $this->_em->createQueryBuilder()
->select(['r', 'option'])
->from('Acme\Domain\Entity\Option\Relation', 'r')
->join('Acme\Domain\Entity\Option\Option', 'option')
;
$subGroups = iterator_to_array($subElement->getElement()->getSubGroups());
return $this->addDirectionWhere($qb, $direction, $inversed)
->andWhere('r.type=:type')
->andWhere(':subGroup MEMBER OF option.subGroups')
->andWhere('option.project=:project')
->setParameter(':type', $type->getValue())
->setParameter(':opt', $option->getId())
->setParameter(':project', $option->getProject())
->setParameter(':subGroup', $subGroups)
->getQuery();
}
/**
* validateDirection checks if the direction given is valid.
*
* #param string $direction
*/
private function validateDirection(string $direction)
{
if (!in_array($direction, $this->directions, true)) {
throw new \InvalidArgumentException(sprintf('Unexpected direction given [%s]. Expected on of: [%s]',
$direction,
implode(', ', $direction)
));
}
}
/**
* addDirectionWhere returns a prepared QueryBuilder with a WHERE on the side needed.
*
* #param $direction
* #return \Doctrine\ORM\QueryBuilder
*/
private function addDirectionWhere(QueryBuilder $qb, $direction, $inversed = false): QueryBuilder
{
switch ($direction) {
case 'right':
$where = $inversed ? 'r.relation<>:opt' : 'r.relation=:opt';
break;
default:
$where = $inversed ? 'r.option<>:opt' : 'r.option=:opt';
break;
}
$qb->where($where);
return $qb;
}
/**
* getRequiredOptions returns a Collection of Relations which are required by the given Option.
*
* #param Option $option
* #param SubElement $subElement
* #return array
*/
public function getRequiredOptions(Option $option, SubElement $subElement): array
{
return $this->getRight($option, $subElement, Type::get(Type::DEPEND))->getResult();
}
/**
* getDependingOptions returns a Collection of Relations which depend on the given Option.
*
* #param Option $option
* #param SubElement $subElement
* #return mixed
*/
public function getDependingOptions(Option $option, SubElement $subElement): array
{
return $this->getLeft($option, $subElement, Type::get(Type::DEPEND))->getResult();
}
/**
* getRequiringOptionsExceptCurrent returns a Collection of Relations which require a Option, except the given Option.
*
* #param Option $option
* #param SubElement $subElement
* #return array
*/
public function getRequiringOptionsExceptCurrent(Option $option, SubElement $subElement): array
{
return $this->getLeft($option, $subElement, Type::get(Type::DEPEND), true)->getResult();
}
/**
* getExceludedOptions returns a Collection of Relations which are excluded by the given Option.
*
* #param Option $option
* #param SubElement $subElement
* #return array
*/
public function getExceludedOptions(Option $option, SubElement $subElement): array
{
return $this->getLeft($option, $subElement, Type::get(Type::EXCLUDE))->getResult();
}
/**
* getExcludedByOptions returns a Collection of Relations which exclude the given Option.
*
* #param Option $option
* #param SubElement $subElement
* #return array
*/
public function getExcludedByOptions(Option $option, SubElement $subElement): array
{
return $this->getRight($option, $subElement, Type::get(Type::EXCLUDE))->getResult();
}
/**
* getSelectedOptions returns a Collection of Relations which are selected by the given Option.
*
* #param Option $option
* #param SubElement $subElement
* #return array
*/
public function getSelectedOptions(Option $option, SubElement $subElement): array
{
return $this->getLeft($option, $subElement, Type::get(Type::SELECT))->getResult();
}
/**
* getSelectedByOptions returns a Collection of Relations which select the given Option.
*
* #param Option $option
* #param SubElement $subElement
* #return array
*/
public function getSelectedByOptions(Option $option, SubElement $subElement): array
{
return $this->getRight($option, $subElement, Type::get(Type::SELECT))->getResult();
}
}
/**
* #ORM\Entity(repositoryClass="Acme\Domain\Entity\Option\RelationRepository")
* #ORM\Table(name="acme_opties_relaties")
*/
class Relation
{
/**
* #ORM\ManyToOne(targetEntity="Acme\Domain\Entity\Option\Option", inversedBy="relations")
* #ORM\JoinColumn(name="id_a", referencedColumnName="id")
* #ORM\Id()
*/
private $option;
/**
* #ORM\OneToOne(targetEntity="Acme\Domain\Entity\Option\Option", fetch="EAGER")
* #ORM\JoinColumn(name="id_b", referencedColumnName="id")
* #ORM\Id()
*/
private $relation;
/**
* Class Option
* #package Acme\Domain\Entity\Option
* #ORM\Entity(repositoryClass="OptionRepository")
* #ORM\Table(name="acme_options")
*/
class Option
{
/**
* #var Collection
* #ORM\OneToMany(targetEntity="Acme\Domain\Entity\Option\Relation", mappedBy="option")
*/
private $relations;
The issue is that the join between the relation and option is not on r.option.id_a = option.id but is on r.option.id_a <> X, it should join on the ids and in the where apply the <> X
Oh well, we still have most of SQL available. So changed it to:
private function getQuery(Option $option, SubElement $subElement, Type $type, string $direction, bool $inversed): Query
{
$direction = strtolower($direction);
$this->validateDirection($direction);
$query = 'select r, option, relation
from Acme\Domain\Entity\Option\Relation r
join r.option option
join r.relation relation
join option.subGroups osg
where osg in (:subGroup)
and option.project = :project
and r.type = :type
';
$query = $this->addDirectionWhere($query, $direction, $inversed);
$subGroups = iterator_to_array($subElement->getElement()->getSubGroups());
$q = $this->_em->createQuery($query);
$q->setParameter(':type', $type->getValue())
->setParameter(':option', $option->getId())
->setParameter(':project', $option->getProject())
->setParameter(':subGroup', $subGroups);
return $q;
}
I am still very open to know what the equivalent would be usin QueryBuilder. Because that has my preference.

Wordpress - Show subtitles to mobile menu from custom field

I am trying to show subtitle to my mobile menu items.
For that I have created a custom field to show as page_subheading for each page.
I can get the value of this custom field using pageId.
But I am not able to append this sub heading to the menu.
I have tried the the below code,
wp_nav_menu( array(
'theme_location' => 'mobile_menu_alt',
'menu_class' => 'dropdown-menu',
'fallback_cb' => false,
'link_after' => '</span><div class="subheading">'.$subheading.'</a></div>',
) );
Here I am calling my menu and adding the subheading after the menu link.
But I am not able to give it dynamically.Is there any way to give value to $subheading dynamically.
Try adding this to your functions.php
class Walker_Nav_Menu_subheading extends Walker {
/**
* What the class handles.
*
* #see Walker::$tree_type
* #since 3.0.0
* #var string
*/
public $tree_type = array( 'post_type', 'taxonomy', 'custom' );
/**
* Database fields to use.
*
* #see Walker::$db_fields
* #since 3.0.0
* #todo Decouple this.
* #var array
*/
public $db_fields = array( 'parent' => 'menu_item_parent', 'id' => 'db_id' );
/**
* Starts the list before the elements are added.
*
* #see Walker::start_lvl()
*
* #since 3.0.0
*
* #param string $output Passed by reference. Used to append additional content.
* #param int $depth Depth of menu item. Used for padding.
* #param array $args An array of arguments. #see wp_nav_menu()
*/
public function start_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "\n$indent<ul class=\"sub-menu\">\n";
}
/**
* Ends the list of after the elements are added.
*
* #see Walker::end_lvl()
*
* #since 3.0.0
*
* #param string $output Passed by reference. Used to append additional content.
* #param int $depth Depth of menu item. Used for padding.
* #param array $args An array of arguments. #see wp_nav_menu()
*/
public function end_lvl( &$output, $depth = 0, $args = array() ) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
}
/**
* Start the element output.
*
* #see Walker::start_el()
*
* #since 3.0.0
* #since 4.4.0 'nav_menu_item_args' filter was added.
*
* #param string $output Passed by reference. Used to append additional content.
* #param object $item Menu item data object.
* #param int $depth Depth of menu item. Used for padding.
* #param array $args An array of arguments. #see wp_nav_menu()
* #param int $id Current item ID.
*/
public function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
/**
* Filter the arguments for a single nav menu item.
*
* #since 4.4.0
*
* #param array $args An array of arguments.
* #param object $item Menu item data object.
* #param int $depth Depth of menu item. Used for padding.
*/
$args = apply_filters( 'nav_menu_item_args', $args, $item, $depth );
/**
* Filter the CSS class(es) applied to a menu item's list item element.
*
* #since 3.0.0
* #since 4.1.0 The `$depth` parameter was added.
*
* #param array $classes The CSS classes that are applied to the menu item's `<li>` element.
* #param object $item The current menu item.
* #param array $args An array of {#see wp_nav_menu()} arguments.
* #param int $depth Depth of menu item. Used for padding.
*/
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args, $depth ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
/**
* Filter the ID applied to a menu item's list item element.
*
* #since 3.0.1
* #since 4.1.0 The `$depth` parameter was added.
*
* #param string $menu_id The ID that is applied to the menu item's `<li>` element.
* #param object $item The current menu item.
* #param array $args An array of {#see wp_nav_menu()} arguments.
* #param int $depth Depth of menu item. Used for padding.
*/
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args, $depth );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
$output .= $indent . '<li' . $id . $class_names .'>';
$atts = array();
$atts['title'] = ! empty( $item->attr_title ) ? $item->attr_title : '';
$atts['target'] = ! empty( $item->target ) ? $item->target : '';
$atts['rel'] = ! empty( $item->xfn ) ? $item->xfn : '';
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
/**
* Filter the HTML attributes applied to a menu item's anchor element.
*
* #since 3.6.0
* #since 4.1.0 The `$depth` parameter was added.
*
* #param array $atts {
* The HTML attributes applied to the menu item's `<a>` element, empty strings are ignored.
*
* #type string $title Title attribute.
* #type string $target Target attribute.
* #type string $rel The rel attribute.
* #type string $href The href attribute.
* }
* #param object $item The current menu item.
* #param array $args An array of {#see wp_nav_menu()} arguments.
* #param int $depth Depth of menu item. Used for padding.
*/
$atts = apply_filters( 'nav_menu_link_attributes', $atts, $item, $args, $depth );
$attributes = '';
foreach ( $atts as $attr => $value ) {
if ( ! empty( $value ) ) {
$value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
$attributes .= ' ' . $attr . '="' . $value . '"';
}
}
/** This filter is documented in wp-includes/post-template.php */
$title = apply_filters( 'the_title', $item->title, $item->ID );
/**
* Filter a menu item's title.
*
* #since 4.4.0
*
* #param string $title The menu item's title.
* #param object $item The current menu item.
* #param array $args An array of {#see wp_nav_menu()} arguments.
* #param int $depth Depth of menu item. Used for padding.
*/
$title = apply_filters( 'nav_menu_item_title', $title, $item, $args, $depth );
$subheading = get_post_meta($item->ID, 'subheading', true);
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . $title . "<span 'subheading'>" . $subheading . "</span>";
$item_output .= '</a>';
$item_output .= $args->after;
/**
* Filter a menu item's starting output.
*
* The menu item's starting output only includes `$args->before`, the opening `<a>`,
* the menu item's title, the closing `</a>`, and `$args->after`. Currently, there is
* no filter for modifying the opening and closing `<li>` for a menu item.
*
* #since 3.0.0
*
* #param string $item_output The menu item's starting HTML output.
* #param object $item Menu item data object.
* #param int $depth Depth of menu item. Used for padding.
* #param array $args An array of {#see wp_nav_menu()} arguments.
*/
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
/**
* Ends the element output, if needed.
*
* #see Walker::end_el()
*
* #since 3.0.0
*
* #param string $output Passed by reference. Used to append additional content.
* #param object $item Page data object. Not used.
* #param int $depth Depth of page. Not Used.
* #param array $args An array of arguments. #see wp_nav_menu()
*/
public function end_el( &$output, $item, $depth = 0, $args = array() ) {
$output .= "</li>\n";
}
} // Walker_Nav_Menu_subheading
and this into your theme file, such as heading.php
wp_nav_menu( array(
'theme_location' => 'mobile_menu_alt',
'menu_class' => 'dropdown-menu',
'fallback_cb' => false,
'walker' => new Walker_Nav_Menu_subheading()
) );
It relies on you using the post_meta name of 'subheading'....
All I did was make a copy of the standard wordPress one and changed how the after bit works.
these lines
$subheading = get_post_meta($item->ID, 'subheading', true);
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . $title . "<span 'subheading'>" . $subheading . "</span>";

PhpStorm Laravel 5 Form/Html not found issue

So I've seen this question a million times around the internet already, but my problem is slightly different.
I've created a new Laravel project and tried to use Html and Form classes. They did not exist so I did everything to include them to my project. And voila! It works... Kinda...
The classes Html and Form seem to be working. If I create a form and refresh in my browser, there's no error and the form is showing. But... PhpStorm does not recognize the classes, it keeps whining about undefined class Html and Form. With this there's the problem that the auto-complete for those classes do not work at all.
I've also included the _ide_helper.php class by 'barryvdh' (found on https://github.com/barryvdh/laravel-ide-helper) But that did not help with this problem.
I'm out of ideas, can anyone help me on this one? Would be greatly appreciated.
Cheers!
This works for me, do you have your composer.json file set up properly?
Make sure the scripts section is set up properly and then run composer dump-autoload.
composer.json
"scripts": {
"post-update-cmd": [
"php artisan clear-compiled",
"php artisan ide-helper:generate",
"php artisan optimize"
] ...
},
Here is what my _ide_helper.php file looks like.
_ide_helper.php
class Form extends \Illuminate\Html\FormFacade{
/**
* Open up a new HTML form.
*
* #param array $options
* #return string
* #static
*/
public static function open($options = array()){
return \Illuminate\Html\FormBuilder::open($options);
}
/**
* Create a new model based form builder.
*
* #param mixed $model
* #param array $options
* #return string
* #static
*/
public static function model($model, $options = array()){
return \Illuminate\Html\FormBuilder::model($model, $options);
}
/**
* Set the model instance on the form builder.
*
* #param mixed $model
* #return void
* #static
*/
public static function setModel($model){
\Illuminate\Html\FormBuilder::setModel($model);
}
/**
* Close the current form.
*
* #return string
* #static
*/
public static function close(){
return \Illuminate\Html\FormBuilder::close();
}
/**
* Generate a hidden field with the current CSRF token.
*
* #return string
* #static
*/
public static function token(){
return \Illuminate\Html\FormBuilder::token();
}
/**
* Create a form label element.
*
* #param string $name
* #param string $value
* #param array $options
* #return string
* #static
*/
public static function label($name, $value = null, $options = array()){
return \Illuminate\Html\FormBuilder::label($name, $value, $options);
}
/**
* Create a form input field.
*
* #param string $type
* #param string $name
* #param string $value
* #param array $options
* #return string
* #static
*/
public static function input($type, $name, $value = null, $options = array()){
return \Illuminate\Html\FormBuilder::input($type, $name, $value, $options);
}
/**
* Create a text input field.
*
* #param string $name
* #param string $value
* #param array $options
* #return string
* #static
*/
public static function text($name, $value = null, $options = array()){
return \Illuminate\Html\FormBuilder::text($name, $value, $options);
}
/**
* Create a password input field.
*
* #param string $name
* #param array $options
* #return string
* #static
*/
public static function password($name, $options = array()){
return \Illuminate\Html\FormBuilder::password($name, $options);
}
/**
* Create a hidden input field.
*
* #param string $name
* #param string $value
* #param array $options
* #return string
* #static
*/
public static function hidden($name, $value = null, $options = array()){
return \Illuminate\Html\FormBuilder::hidden($name, $value, $options);
}
/**
* Create an e-mail input field.
*
* #param string $name
* #param string $value
* #param array $options
* #return string
* #static
*/
public static function email($name, $value = null, $options = array()){
return \Illuminate\Html\FormBuilder::email($name, $value, $options);
}
/**
* Create a url input field.
*
* #param string $name
* #param string $value
* #param array $options
* #return string
* #static
*/
public static function url($name, $value = null, $options = array()){
return \Illuminate\Html\FormBuilder::url($name, $value, $options);
}
/**
* Create a file input field.
*
* #param string $name
* #param array $options
* #return string
* #static
*/
public static function file($name, $options = array()){
return \Illuminate\Html\FormBuilder::file($name, $options);
}
/**
* Create a textarea input field.
*
* #param string $name
* #param string $value
* #param array $options
* #return string
* #static
*/
public static function textarea($name, $value = null, $options = array()){
return \Illuminate\Html\FormBuilder::textarea($name, $value, $options);
}
/**
* Create a select box field.
*
* #param string $name
* #param array $list
* #param string $selected
* #param array $options
* #return string
* #static
*/
public static function select($name, $list = array(), $selected = null, $options = array()){
return \Illuminate\Html\FormBuilder::select($name, $list, $selected, $options);
}
/**
* Create a select range field.
*
* #param string $name
* #param string $begin
* #param string $end
* #param string $selected
* #param array $options
* #return string
* #static
*/
public static function selectRange($name, $begin, $end, $selected = null, $options = array()){
return \Illuminate\Html\FormBuilder::selectRange($name, $begin, $end, $selected, $options);
}
/**
* Create a select year field.
*
* #param string $name
* #param string $begin
* #param string $end
* #param string $selected
* #param array $options
* #return string
* #static
*/
public static function selectYear(){
return \Illuminate\Html\FormBuilder::selectYear();
}
/**
* Create a select month field.
*
* #param string $name
* #param string $selected
* #param array $options
* #param string $format
* #return string
* #static
*/
public static function selectMonth($name, $selected = null, $options = array(), $format = '%B'){
return \Illuminate\Html\FormBuilder::selectMonth($name, $selected, $options, $format);
}
/**
* Get the select option for the given value.
*
* #param string $display
* #param string $value
* #param string $selected
* #return string
* #static
*/
public static function getSelectOption($display, $value, $selected){
return \Illuminate\Html\FormBuilder::getSelectOption($display, $value, $selected);
}
/**
* Create a checkbox input field.
*
* #param string $name
* #param mixed $value
* #param bool $checked
* #param array $options
* #return string
* #static
*/
public static function checkbox($name, $value = 1, $checked = null, $options = array()){
return \Illuminate\Html\FormBuilder::checkbox($name, $value, $checked, $options);
}
/**
* Create a radio button input field.
*
* #param string $name
* #param mixed $value
* #param bool $checked
* #param array $options
* #return string
* #static
*/
public static function radio($name, $value = null, $checked = null, $options = array()){
return \Illuminate\Html\FormBuilder::radio($name, $value, $checked, $options);
}
/**
* Create a HTML reset input element.
*
* #param string $value
* #param array $attributes
* #return string
* #static
*/
public static function reset($value, $attributes = array()){
return \Illuminate\Html\FormBuilder::reset($value, $attributes);
}
/**
* Create a HTML image input element.
*
* #param string $url
* #param string $name
* #param array $attributes
* #return string
* #static
*/
public static function image($url, $name = null, $attributes = array()){
return \Illuminate\Html\FormBuilder::image($url, $name, $attributes);
}
/**
* Create a submit button element.
*
* #param string $value
* #param array $options
* #return string
* #static
*/
public static function submit($value = null, $options = array()){
return \Illuminate\Html\FormBuilder::submit($value, $options);
}
/**
* Create a button element.
*
* #param string $value
* #param array $options
* #return string
* #static
*/
public static function button($value = null, $options = array()){
return \Illuminate\Html\FormBuilder::button($value, $options);
}
/**
* Get the ID attribute for a field name.
*
* #param string $name
* #param array $attributes
* #return string
* #static
*/
public static function getIdAttribute($name, $attributes){
return \Illuminate\Html\FormBuilder::getIdAttribute($name, $attributes);
}
/**
* Get the value that should be assigned to the field.
*
* #param string $name
* #param string $value
* #return string
* #static
*/
public static function getValueAttribute($name, $value = null){
return \Illuminate\Html\FormBuilder::getValueAttribute($name, $value);
}
/**
* Get a value from the session's old input.
*
* #param string $name
* #return string
* #static
*/
public static function old($name){
return \Illuminate\Html\FormBuilder::old($name);
}
/**
* Determine if the old input is empty.
*
* #return bool
* #static
*/
public static function oldInputIsEmpty(){
return \Illuminate\Html\FormBuilder::oldInputIsEmpty();
}
/**
* Get the session store implementation.
*
* #return \Illuminate\Session\Store $session
* #static
*/
public static function getSessionStore(){
return \Illuminate\Html\FormBuilder::getSessionStore();
}
/**
* Set the session store implementation.
*
* #param \Illuminate\Session\Store $session
* #return $this
* #static
*/
public static function setSessionStore($session){
return \Illuminate\Html\FormBuilder::setSessionStore($session);
}
/**
* Register a custom macro.
*
* #param string $name
* #param callable $macro
* #return void
* #static
*/
public static function macro($name, $macro){
\Illuminate\Html\FormBuilder::macro($name, $macro);
}
/**
* Checks if macro is registered.
*
* #param string $name
* #return bool
* #static
*/
public static function hasMacro($name){
return \Illuminate\Html\FormBuilder::hasMacro($name);
}
}
class Html extends \Illuminate\Html\HtmlFacade{
/**
* Convert an HTML string to entities.
*
* #param string $value
* #return string
* #static
*/
public static function entities($value){
return \Illuminate\Html\HtmlBuilder::entities($value);
}
/**
* Convert entities to HTML characters.
*
* #param string $value
* #return string
* #static
*/
public static function decode($value){
return \Illuminate\Html\HtmlBuilder::decode($value);
}
/**
* Generate a link to a JavaScript file.
*
* #param string $url
* #param array $attributes
* #param bool $secure
* #return string
* #static
*/
public static function script($url, $attributes = array(), $secure = null){
return \Illuminate\Html\HtmlBuilder::script($url, $attributes, $secure);
}
/**
* Generate a link to a CSS file.
*
* #param string $url
* #param array $attributes
* #param bool $secure
* #return string
* #static
*/
public static function style($url, $attributes = array(), $secure = null){
return \Illuminate\Html\HtmlBuilder::style($url, $attributes, $secure);
}
/**
* Generate an HTML image element.
*
* #param string $url
* #param string $alt
* #param array $attributes
* #param bool $secure
* #return string
* #static
*/
public static function image($url, $alt = null, $attributes = array(), $secure = null){
return \Illuminate\Html\HtmlBuilder::image($url, $alt, $attributes, $secure);
}
/**
* Generate a HTML link.
*
* #param string $url
* #param string $title
* #param array $attributes
* #param bool $secure
* #return string
* #static
*/
public static function link($url, $title = null, $attributes = array(), $secure = null){
return \Illuminate\Html\HtmlBuilder::link($url, $title, $attributes, $secure);
}
/**
* Generate a HTTPS HTML link.
*
* #param string $url
* #param string $title
* #param array $attributes
* #return string
* #static
*/
public static function secureLink($url, $title = null, $attributes = array()){
return \Illuminate\Html\HtmlBuilder::secureLink($url, $title, $attributes);
}
/**
* Generate a HTML link to an asset.
*
* #param string $url
* #param string $title
* #param array $attributes
* #param bool $secure
* #return string
* #static
*/
public static function linkAsset($url, $title = null, $attributes = array(), $secure = null){
return \Illuminate\Html\HtmlBuilder::linkAsset($url, $title, $attributes, $secure);
}
/**
* Generate a HTTPS HTML link to an asset.
*
* #param string $url
* #param string $title
* #param array $attributes
* #return string
* #static
*/
public static function linkSecureAsset($url, $title = null, $attributes = array()){
return \Illuminate\Html\HtmlBuilder::linkSecureAsset($url, $title, $attributes);
}
/**
* Generate a HTML link to a named route.
*
* #param string $name
* #param string $title
* #param array $parameters
* #param array $attributes
* #return string
* #static
*/
public static function linkRoute($name, $title = null, $parameters = array(), $attributes = array()){
return \Illuminate\Html\HtmlBuilder::linkRoute($name, $title, $parameters, $attributes);
}
/**
* Generate a HTML link to a controller action.
*
* #param string $action
* #param string $title
* #param array $parameters
* #param array $attributes
* #return string
* #static
*/
public static function linkAction($action, $title = null, $parameters = array(), $attributes = array()){
return \Illuminate\Html\HtmlBuilder::linkAction($action, $title, $parameters, $attributes);
}
/**
* Generate a HTML link to an email address.
*
* #param string $email
* #param string $title
* #param array $attributes
* #return string
* #static
*/
public static function mailto($email, $title = null, $attributes = array()){
return \Illuminate\Html\HtmlBuilder::mailto($email, $title, $attributes);
}
/**
* Obfuscate an e-mail address to prevent spam-bots from sniffing it.
*
* #param string $email
* #return string
* #static
*/
public static function email($email){
return \Illuminate\Html\HtmlBuilder::email($email);
}
/**
* Generate an ordered list of items.
*
* #param array $list
* #param array $attributes
* #return string
* #static
*/
public static function ol($list, $attributes = array()){
return \Illuminate\Html\HtmlBuilder::ol($list, $attributes);
}
/**
* Generate an un-ordered list of items.
*
* #param array $list
* #param array $attributes
* #return string
* #static
*/
public static function ul($list, $attributes = array()){
return \Illuminate\Html\HtmlBuilder::ul($list, $attributes);
}
/**
* Build an HTML attribute string from an array.
*
* #param array $attributes
* #return string
* #static
*/
public static function attributes($attributes){
return \Illuminate\Html\HtmlBuilder::attributes($attributes);
}
/**
* Obfuscate a string to prevent spam-bots from sniffing it.
*
* #param string $value
* #return string
* #static
*/
public static function obfuscate($value){
return \Illuminate\Html\HtmlBuilder::obfuscate($value);
}
/**
* Register a custom macro.
*
* #param string $name
* #param callable $macro
* #return void
* #static
*/
public static function macro($name, $macro){
\Illuminate\Html\HtmlBuilder::macro($name, $macro);
}
/**
* Checks if macro is registered.
*
* #param string $name
* #return bool
* #static
*/
public static function hasMacro($name){
return \Illuminate\Html\HtmlBuilder::hasMacro($name);
}
}
EDIT
Do you have you config/app.php and composer.json files set up properly?
app.php
'providers' => [
...
Barryvdh\LaravelIdeHelper\IdeHelperServiceProvider::class,
Illuminate\Html\HtmlServiceProvider::class,
...
];
'aliases' => [
...
'Form' => Illuminate\Html\FormFacade::class,
'Html' => Illuminate\Html\HtmlFacade::class,
...
];
composer.json
"require": {
...
"illuminate/html": "~5.0",
...
},
"require-dev": {
...
"barryvdh/laravel-ide-helper": "~2.0",
...
},

Symfony2 SonataClassificationBundle querying from database

I am trying to query a dynamic category sidebar and I am using the Sonata ClassificationBundle. I Have created a few categories(parent) and some subcategories(child). I am able to show the categories, however I cant understand how to query the subcategories under a specific category. I think i need to check if the category has children and display it? I have no idea how to do that without proper documentation..
This is the sidebar controller:
<?php
namespace Mp\ShopBundle\Controller;
use Sonata\ClassificationBundle\Entity\Category;
use Sonata\ClassificationBundle\Model\CategoryInterface;
use Sonata\Component\Currency\CurrencyDetector;
use Sonata\Component\Product\Pool;
use Sonata\ProductBundle\Entity\ProductSetManager;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
class SidebarController extends Controller
{
/**
* # Route("/hello/{name}")
* # Template()
*/
public function sidebarAction()
{
$page = $this->getRequest()->get('page', 1);
$displayMax = $this->getRequest()->get('max', 9);
$displayMode = $this->getRequest()->get('mode', 'grid');
$filter = $this->getRequest()->get('filter');
$option = $this->getRequest()->get('option');
if (!in_array($displayMode, array('grid'))) { // "list" mode will be added later
throw new NotFoundHttpException(sprintf('Given display_mode "%s" doesn\'t exist.', $displayMode));
}
$category = $this->retrieveCategoryFromQueryString();
return $this->render('sidebar.html.twig', array(
'display_mode' => $displayMode,
'category' => $this->getCategoryManager()->findBy(
array('parent' => 1 )),
'subcategory' => $this->getCategoryManager()->findBy(
array('children' => true )), ////// ERROR You cannot search for the association field 'Application\Sonata\ClassificationBundle\Entity\Category#children', because it is the inverse side of an association. Find methods only work on owning side associations."
'provider' => $this->getProviderFromCategory($category),
));
}
/* $em = $this->getDoctrine()->getManager();
$products = $em->getRepository('MpShopBundle:Product')->findAll();
return $this->render('sidebar.html.twig', array(
'products'=>$products
)); */
/**
* Retrieve Category from its id and slug, if any.
*
* #return CategoryInterface|null
*/
protected function retrieveCategoryFromQueryString()
{
$categoryId = $this->getRequest()->get('category_id');
$categorySlug = $this->getRequest()->get('category_slug');
if (!$categoryId || !$categorySlug ) {
return null;
}
return $this->getCategoryManager()->findOneBy(array(
'id' => $categoryId,
'enabled' => true,
));
}
This is how I try to display the sidebar:
{% for categories in category %}
{% for subcategories in subcategory %}
<li class="subMenu"><a> {{ categories.name }} [{{ categories|length }}]</a>
<ul>
<li>{{ subcategories.name }} ({{ categories|length }})</li>
</ul>
</li>
{% endfor %}
{% endfor %}
The Category entity:
<?php
/*
* This file is part of the Sonata project.
*
* (c) Sonata Project <https://github.com/sonata-project/SonataClassificationBundle/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\ClassificationBundle\Model;
use Sonata\MediaBundle\Model\MediaInterface;
interface CategoryInterface
{
/**
* #param $name
*
* #return mixed
*/
public function setName($name);
/**
* Get name
*
* #return string $name
*/
public function getName();
/**
* Set enabled
*
* #param boolean $enabled
*/
public function setEnabled($enabled);
/**
* Get enabled
*
* #return boolean $enabled
*/
public function getEnabled();
/**
* Set slug
*
* #param integer $slug
*/
public function setSlug($slug);
/**
* Get slug
*
* #return integer $slug
*/
public function getSlug();
/**
* Set description
*
* #param string $description
*/
public function setDescription($description);
/**
* Get description
*
* #return string $description
*/
public function getDescription();
/**
* #param integer $position
*/
public function setPosition($position);
/**
* #return integer
*/
public function getPosition();
/**
* Add Children
*
* #param CategoryInterface $children
* #param boolean $nested
*/
public function addChild(CategoryInterface $children, $nested = false);
/**
* Get Children
*
* #return \Doctrine\Common\Collections\Collection $children
*/
public function getChildren();
/**
* Set children
*
* #param $children
*/
public function setChildren($children);
/**
* Return true if category has children
*
* #return boolean
*/
public function hasChildren();
/**
* Set Parent
*
* #param CategoryInterface $parent
* #param boolean $nested
*/
public function setParent(CategoryInterface $parent = null, $nested = false);
/**
* Get Parent
*
* #return CategoryInterface $parent
*/
public function getParent();
/**
* #param MediaInterface $media
*/
public function setMedia(MediaInterface $media = null);
/**
* #return MediaInterface
*/
public function getMedia();
/**
* #param ContextInterface $context
*/
public function setContext(ContextInterface $context);
/**
* #return ContextInterface
*/
public function getContext();
}
MY IDEA
Okay since all the parent categories have a parent_id of 1, i display them easily. The child categories have parent_id that is the same as the id of the parent category. Is it possible to somehow query this condition? Like parent_id = id?
You should simply use getChildren() on every parent category: the entity should be hydrated, so the query is already done for you.

Categories