Error 500 "View Not Found" when using SEF and URL Rewriting - joomla

I am writing a custom component. I have the view employees. Under this view, I have two layouts, default and modal.
I have a menu item in the toplevel of the main menu, Employees that points to my employee view:
index.php?option=com_mycomponent&view=employees which resolves to domain.com/joomla/employees and displayes the default view as expected.
Now, inside my component I want to link to the modal view, and I do so using JRoute and this url:
index.php?option=com_mycomponent&view=employees&layout=modal
Which resolves to
domain.com/joomla/employees/modal
And produces this error:
500 - View not found [name, type, prefix]: modal, html,
mycomponentView
If I visit index.php using index.php?option=com_mycomponent&view=employees&layout=modal my modal view is displayed.
I have also found that visiting domain.com/joomla/employees/employees/modal displays the correct layout. It is as if joomla is forgetting what view is associated with the menu item at /joomla/employees, and instead looks for the view "modal" unless the extra "employees" is provided in the url.
Also worth noting, domain.com/joomla/employee?layout=modal works fine as well.
Here is what I have for my router.php. This was file was generated for me using the component generator at j-cook.pro.
<?php
defined('_JEXEC') or die;
function MycomponentBuildRoute(&$query){
$segments = array();
if(isset($query['view']))
{
$view = $query['view'];
$segments[] = $view;
unset( $query['view'] );
}
if(isset($query['layout']))
{
$segments[] = $query['layout'];
unset( $query['layout'] );
}
if(isset($query['id']))
{
if(in_array($view, array('edit','view','view','editfacility','view','edit','client','editclient','viewposition','editposition','edit','view','edit','view','view','edit','view','edit','view','edit','view','edit')))
{
$segments[] = (is_array($query['id'])?implode(',', $query['id']):$query['id']);
unset( $query['id'] );
}
};
return $segments;
}
function MycomponentParseRoute($segments)
{
$vars = array();
$vars['view'] = $segments[0];
$nextPos = 1;
if (isset($segments[$nextPos]))
{
$vars['layout'] = $segments[$nextPos];
$nextPos++;
}
if(in_array($vars['view'], array('edit','view','view','editfacility','view','edit','client','editclient','viewposition','editposition','edit','view','edit','view','view','edit','view','edit','view','edit','view','edit'))
&& isset($segments[$nextPos]))
{
$slug = $segments[$nextPos];
$id = explode( ':', $slug );
$vars['id'] = (int) $id[0];
$nextPos++;
}
return $vars;
}

So it is hard to provide an exact answer for this without knowing all the different ways that you want to have urls be parsed. But I will try to give a hint at what will solve this present situation (without hopefully introducing too many new issues!)
The basic issue is that the BuildRoute side does not get a view value so it is not included in the url. On the one hand it is not necessary, because it is in the menu. But it makes it a little harder to parse, so option one is to force there to be a view if you can by changing the top function to start like this:
function MycomponentBuildRoute(&$query){
$segments = array();
if(isset($query['view']))
{
$view = $query['view'];
$segments[] = $view;
unset( $query['view'] );
}
else
{
$app = JFactory::getApplication();
$menu = $app->getMenu();
$active = $menu->getActive();
if ($view = $active->query['view']) {
$segments[] = $view;
}
}
...
In this way, if there is a menu item for this and it has a view we will tack it on. This should generate domain.com/joomla/employees/employees/modal.
You could also probably do this logic on the parse side too. This would go instead of the other option above:
function MycomponentParseRoute($segments)
{
$vars = array();
$app = JFactory::getApplication();
$menu = $app->getMenu();
$active = $menu->getActive();
if ($active->query['view']) {
$vars['layout'] = $segments[0];
$nextPos = 1;
} else {
$vars['view'] = $segments[0];
$nextPos = 1;
if (isset($segments[$nextPos]))
{
$vars['layout'] = $segments[$nextPos];
$nextPos++;
}
}
... continue with final check for id
I would probably use the second option but both are an option. By the way, you are also likely to run into issues if you try to use an id without setting a layout.

Related

joomla - router change url when getting the name of product

I have build my own component in joomla and client wants now a friendly urls f.e
website.com/someplace/{product-id}-{product-name}. So i Build my own router like this.
function componentBuildRoute(&$query)
{
$segments = [];
if (isset($query['view'])) {
$segments[] = "szkolenie";
unset($query['view']);
}
if (isset($query['product_id'])) {
$productName = JFilterOutput::stringURLSafe(strtolower(getProductName($query['product_id'])));
$newName = $query['product_id'] . '-' . $productName;
$segments[] = $newName;
unset($query['product_id']);
}
return $segments;
}
and parse route function
function componentParseRoute($segments)
{
$app = JFactory::getApplication();
$menu = $app->getMenu();
$item =& $menu->getActive();
$count = count($segments);
switch ($item->query['view']) {
case 'catalogue' : {
$view = 'training';
$id = $segments[1];
}
break;
}
$data = [
'view' => $view,
'product_id' => $id
];
return $data;
}
While on the end of buildroute function segments are ok I have exactly what I want that on the beginning of parse route I have something like
website.com/szkolenie/1-krakow <-- I dont know wtf is this krakow( I know it is city i Poland) but still where is it get from ? The getProductName function implementation is
function getProductName($productId)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('#__component_training.id as id, #__component_product' . name)
->from($db->quoteName('#__component_training'))
->where('#__s4edu_product.product_id = ' . $productId)
->leftJoin('#__component_product ON
#__component_training.product_id=#__component_product.product_id');
$training = $db->loadObject();
return trim($training->name);
}
So taking all this into consideration I think that something is happening between the buildRoute and parseRoute, something what filters the $segment[1] variable, but how to disable that and why is it happening ?
P.S
Please do not send me to https://docs.joomla.org/Joomla_Routes_%26_SEF
I already know all the tutorials on joomla website which contains anything with sef.
P.S.S
It is built on joomla 3.7.0
You do not have a product named "krakow" ?
If not you can try to remove the $productName from the build function, just to check if this "krakow" is added automaticaly or it's from the getProductName() function.
Also i noticed that you have an error i guess in the function getProductName()
->where('#__s4edu_product.product_id = ' . $productId)
It's should be
->where('#__component_product.product_id = ' . $productId)

Simple way to add category link in layered navigation so that category open with the actual url insted filter

I want to add link category link in layered navigation of Magento so that when any visitor click on category in layered navigation it will open with their actual url instead of filtering the results on same page.
I found this : https://magento.stackexchange.com/questions/9513/layered-navigation-category-link
but this is little bit confusing. Anyone have any simple way to do this modification?
I know how remove category filter list from layered navigation of each category and now I want to know how can I add any static block above the filters in layered navigation so that I can add subcategory link of every category.
Please give some suggestion.
All you need to do is using layout handle <catalog_category_layered /> add a block in left. and on that phtml file using current category get all subcategories.
in your local.xml file
<layout>
<catalog_category_layered>
<block type="core/template" name="layeredcatnav" template="catalog/navigation/layeredcatnav.phtml"/>
</catalog_category_layered>
</layout>
And inside catalog/navigation/layeredcatnav.phtml file
<?php $current_cat = Mage::getSingleton('catalog/layer')->getCurrentCategory();
$cat_id = $current_cat->getId(); //current category id
//now get subcategories using this category id
$children = Mage::getModel('catalog/category')->getCategories($cat_id);
foreach ($children as $category) {
echo $category->getName();
echo $category->getURL();
}
?>
Note : not tested
This can be fixed quite easily by overriding the getUrl() method in the Mage_Catalog_Model_Layer_Filter_Item class
Go to /app/code/core/Mage/Catalog/Model/Layer/Filter/Item.php
public function getUrl()
{
$query = array(
$this->getFilter()->getRequestVar()=>$this->getValue(),
Mage::getBlockSingleton('page/html_pager')->getPageVarName() => null // exclude current page from urls);
return Mage::getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true, '_query'=>$query));
}
Change to :
public function getUrl()
{
if($this->getFilter()->getRequestVar() == "cat"){
$category_url = Mage::getModel('catalog/category')->load($this->getValue())->getUrl();
$return = $category_url;
$request = Mage::getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true));
if(strpos($request,'?') !== false ){
$query_string = substr($request,strpos($request,'?'));
}
else{
$query_string = '';
}
if(!empty($query_string)){
$return .= $query_string;
}
return $return;
}
else{
$query = array(
$this->getFilter()->getRequestVar()=>$this->getValue(),
Mage::getBlockSingleton('page/html_pager')->getPageVarName() => null // exclude current page from urls
);
return Mage::getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true, '_query'=>$query));
}
}
If It's work for you then please copy Item.php file and put local file on this path /app/code/local/Mage/Catalog/Model/Layer/Filter/Item.php
It's possible you can you this code for magento 2.
For magento 2, we have the same function in vendor\magento\module-catalog\Model\Layer\Filter\Item.php we have getUrl() function.
the orignal code:
public function getUrl()
{
$query = [
$this->getFilter()->getRequestVar() => $this->getValue(),
// exclude current page from urls
$this->_htmlPagerBlock->getPageVarName() => null,
];
return $this->_url->getUrl('*/*/*', ['_current' => true, '_use_rewrite' => true, '_query' => $query]);
}
and then the custom code to solve the issue:
public function getUrl()
{
if($this->getFilter()->getRequestVar() == "cat"){
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$category_url = $objectManager->create('Magento\Catalog\Model\Category')->load($this->getValue())->getUrl();
$return = $category_url;
$request = $this->_url->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true));
if(strpos($request,'?') !== false ){
$query_string = substr($request,strpos($request,'?'));
}
else{
$query_string = '';
}
if(!empty($query_string)){
$return .= $query_string;
}
return $return;
}
else{
$query = array(
$this->getFilter()->getRequestVar()=>$this->getValue(),
$this->_htmlPagerBlock->getPageVarName() => null // exclude current page from urls
);
return $this->_url->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true, '_query'=>$query));
}
}
It's working only category section. It's working fine in my case.

Advanced Custom Fields—Custom location rule that matches custom taxonomy

I am using the Advanced Custom Fields plugin on a WordPress site. I am aiming to have a custom location rule to show the custom filed group when any term within a custom taxonomy is selected.
Following the custom location rule tutorial on the ACF site I have added custom types, operators, and values to the rules row.
My matching function (code below) works only when the page is reloaded, but not via AJAX. How do I add custom taxonomies to the $options array so the matching function can evaluate via AJAX when custom taxonomy terms are checked/unchecked.
function acf_location_rules_match_taxonomyTerm( $match, $rule, $options ){
// vars
$taxonomies = get_object_taxonomies( $options['post_type'] );
$terms = $options['taxonomy'];
// not AJAX
if( !$options['ajax'] ){
// no terms? Load them from the post_id
if( empty($terms) ){
if( is_array($taxonomies) ){
foreach( $taxonomies as $tax ){
$all_terms = get_the_terms( $options['post_id'], $tax );
if($all_terms){
foreach($all_terms as $all_term){
$terms[] = $all_term->term_id;
}
}
}
}
}
if($rule['operator'] == "<==>"){
$match = false;
if($terms){
$current_terms = get_the_terms($options['post_id'], $rule['value']);
if ( $current_terms && ! is_wp_error( $terms ) ) {
$current_term_ids = array();
foreach ($current_terms as $current_term) {
$current_term_ids[] = $current_term->term_id;
}
}
foreach ($current_term_ids as $current_term_id) {
if( in_array($current_term_id, $terms) ){
$match = true;
}
}
}
}
else{
$match = false;
}
}
return $match;
}
Have you tried this using this plugin? might help. though its a bit late. :)
https://wordpress.org/plugins/advanced-custom-fields-meta-location-rule/

Adding attribute options programmatically are not available for use immediately

I'm creating Magento attribute options via a script, but I need to then be able to get the new ID and use it straight away in the same script.
At the moment it's not pulling the id through - if I kill the script and re-start it it picks up the created option and returns the ID, but not as part of the same script.
Here is the code I am using:
$attr = Mage::getModel('catalog/product')->getResource()->getAttribute($key);
if ($attr->usesSource()) {
$vattr_id = $attr->getSource()->getOptionId($value);
}else{
echo "No Source";
$vattr_id = false;
}
if($vattr_id){
return $vattr_id;
}else{
$attr_model = Mage::getModel('catalog/resource_eav_attribute');
$attr = $attr_model->loadByCode('catalog_product', $key);
$attr_id = $attr->getAttributeId();
$option['attribute_id'] = $attr_id;
$option['value'][$value][0] = $value;
$option['value'][$value][1] = $value;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($option);
$attr = Mage::getModel('catalog/product')->getResource()->getAttribute($key);
if ($attr->usesSource()) {
$vattr_id = $attr->getSource()->getOptionId($value);
echo "AttrID: $vattr_id";
}
}
Running this (with the required Mage::app() etc), creates the option, you can see it in the Magento back end, but the $vattr_id is NULL. If I reload the script, then it finds the attribute option in that first block as it should.
I guess it's something to do with how Magento is caching the models, but not sure where I need to look to clear these?
function getAttributeOptionId($attributeName, $attributeValue) {
/* #var $attribute Mage_Eav_Model_Entity_Attribute */
$attribute = Mage::getModel("eav/entity_attribute")->loadByCode("catalog_product", $attributeName);
// checking attribute code
if ($attribute->getId()) {
$source = $attribute->getSource();
$options = $source->getAllOptions();
// looking for existing id
foreach ($options as $optionValue) {
if ($attributeValue == $optionValue["label"]) {
return $optionValue["value"];
}
}
// making new option
$addOptionData = array(
"attribute_id" => $attribute->getId(),
"value" => array(array($attributeValue))
);
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttributeOption($addOptionData);
// getting new id
$attribute = Mage::getModel("eav/entity_attribute")->loadByCode("catalog_product", $attributeName);
$source = $attribute->getSource();
$options = $source->getAllOptions();
foreach ($options as $optionValue) {
if ($attributeValue == $optionValue["label"]) {
return $optionValue["value"];
}
}
}
return null;
}
echo getAttributeOptionId("brand", "Intel");

Redirect , POST and Flashdata issue in Codeigniter

i am developing an application where i need some suggestions. Here is the detail of the problem.
public function form()
{
$this->load->helper('inflector');
$id = $this->uri->segment(3,0);
if($data = $this->input->post()){
$result = $this->form_validation->run();
if($result){
if($id > 0){
// here update code
}else{
$this->mymodel->insert($data);
$this->session->set_flashdata('message','The page has been added successfully.');
$this->redirect = "mycontroller/index";
$this->view = FALSE;
}
}else{
//$this->call_post($data);
$this->session->set_flashdata('message','The Red fields are required');
$this->view = FALSE;
$this->redirect = "mycontroller/form/$id";
}
}else{
$row = $this->mymodel->fetch_row($id);
$this->data[]= $row;
}
}
public function _remap($method, $parameters)
{
if (method_exists($this, $method))
{
$return = call_user_func_array(array($this, $method),$parameters);
}else{
show_404();
}
if(strlen($this->view) > 0)
{
$this->template->build('default',array());
}else{
redirect($this->redirect);
}
}
Here you can see how i am trying to reload the page on failed validation.
Now the problem is that i have to display the flash data on the view form which is only available after redirect and i need to display the validation errors to which are not being displayed on redirect due to the loss of post variable. If i dont use redirect then cant display flashdata but only validation errors. I want both of the functionalities togather. I have tried even creating POSt again like this
public function call_post($data)
{
foreach($data as $key => $row){
$_POST[$key] = $row;
}
}
Which i commented out in the formmethod.How can i achieve this.
Here's a thought.
I think you can add the validation error messages into the flash data. Something like this should work:
$this->session->set_flashdata('validation_error_messages',validation_errors());
Notice the call to the validation_errors function. This is a bit unconventional, but I think it should work. Just make sure that the code are executed after the statement $this->form_validation->run(); to make sure the validation error messages are produced by the Form Validation library.
well i have little different approach hope will help you here it is
mycontroller extend CI_Controller{
function _remap($method,$params){
switch($method){
case 'form':
$this->form($params);
break;
default:
$this->index();
break;
}
}
function form(){
$this->load->helper('inflector');
$id = $this->uri->segment(3,0);
$this->form_validation->set_rules('name','Named','required|trim');
$this->form_validation->set_rules('email','email','required|valid_email|trim');
// if validation fails and also for first time form called
if(!$this->form_validation->run()){
$this->template->build('default',array());
}
else{ // validation passed
$this->save($id)
}
}
function save($id = 0){
$data = $this->input->post();
if($id == 0){
$this->mymodel->insert($data);
$this->session->set_flashdata('message','The page has been added successfully.');
$this->redirect = "mycontroller/index";
$this->view = FALSE;
}else{
// update the field
$this->session->set_flashdata('message','The Red fields are required');
}
redirect("mycontroller/index");
}
}
your form/default view should be like this
<?
if(validation_errors())
echo validation_errors();
elseif($this->session->flashdata('message'))
echo $this->session->flashdata('message');
echo form_open(uri_string());// post data to same url
echo form_input('name',set_value('name'));
echo form_input('email',set_value('email'));
echo form_submit('submit');
echo form_close();
try it if you face any problem post here.

Resources