Custom View depending field value

If any one can point me in the right direction, that would be great. I am sure this must have been done before.

Is it possible to show a custom details view for a module depending on a field in the module.

So for instance if the field value was a 'plot order, it would show the plot detail view, if it was “repair order” it shows the repair detail view.

Thanks
Pete

Take a look into these posts for some ideas:

Hope it helps.

Thanks,

BrozTechnologies

1 Like

Hi,

Here are the functions we use for these kind of customizations:

	function hideField(fld){
		$('#'+fld).closest('.edit-view-row-item').hide();
	}

	function showField(fld){
		$('#'+fld).closest('.edit-view-row-item').show();
	}

	function addRequiredStar(fld){
		$('#'+fld).closest('.edit-view-row-item').find('.label').find('span.required').remove(); // remove it in case it was already there
		$('#'+fld).closest('.edit-view-row-item').find('.label').append('<span class="required">*</span>');
	}

	function removeRequiredStar(fld){
		$('#'+fld).closest('.edit-view-row-item').find('.label').find('span.required').remove();
	}

	function hidePanel(panel){
		$('[data-id="'+panel+'"]').closest('.panel-default').hide()
	}
	function showPanel(panel){
		$('[data-id="'+panel+'"]').closest('.panel-default').show()
	}
	function togglePanel(panel){
		$('[data-id="'+panel+'"]').closest('.panel-default').find('[data-toggle="collapse-edit"]').click()
	}
	function expandPanel(panel){
		$('[data-id="'+panel+'"]').closest('.panel-default').find('.collapsed').click()
	}
	function collapsePanel(panel){
		$('[data-id="'+panel+'"]:visible').closest('.panel-default').find('[data-toggle="collapse-edit"]').click()
	}

2 Likes

@peted
You can write custom field type and addition it there:
include/SugarFields/Fields

Hi,
This is something straightforward if you know the coding.
You need to create 2 different views.
1 for Plot Order
2 for Repair Order

So you have to

Modify Controller.php
Create views/view.plotorder.detail.php & views/view.repairorder.detail.php
Create metadata/plotorderdefs.php & metadata/repairorderdefs.php

In the metadata files you can add the fields which you want to show in each of the view. For simplicity you can copy the detailviewdefs.php file and modify it.

Check this article for the help to create a custom view.

Now ====>
You can add a condition in view/view.detail.php in preDisplay function to check the values. if the values are related to plot view you can redirect the control to Plot View or to Repair view.

If you are a developer then this is enough help for you. If you are not a developer then it is not your game…hire someone to do it for you.

Thanks

Thanks every one, I have looked at the responses and have made progress, I have it working for the edit view but not the details view.

For thew edit view file custom/modules/CoMO_Orders/views/view.edit.php I have thiws code what works well.

<?php

require_once ‘include/MVC/View/views/view.edit.php’;

class CoMO_OrdersViewEdit extends ViewEdit
{
// function runs before view is disaplyed.
public function preDisplay()
{

    //echo $_POST['record'];
    //get the record ID file
    $bean = BeanFactory::getBean('CoMO_Orders', $_POST['record']);
    //chec the order type, if it is not a plot (or related order), load quickcreate view.
    if ($bean->order_type == "Repair" || $bean->order_type == "Other" || $bean->order_type == "Stock") {

        //load smalledit view.
        header('Location: /index.php?module=CoMO_Orders&action=SmallEdit&record=' . $bean->id);
        exit;

    } else {
        //contiue to load this view.
        $this->ev = new EditView();
        $this->ev->ss = &$this->ss;
        $this->ev->view = "EditView";
        $metadataFile = "modules/CoMO_Orders/metadata/editviewdefs.php";
        $this->ev->setup($this->module, $this->bean, $metadataFile, 'include/EditView/EditView.tpl', true, 'editviewdefs');
    }
}

}

For the Details view, I have tried to copy it so i have this /var/www/html/custom/modules/CoMO_Orders/views/view.detail.php

<?php

require_once ‘include/MVC/View/views/view.detail.php’;

class CoMO_OrdersViewDetail extends ViewDetail
{
// function runs before view is disaplyed.
public function preDisplay()
{

    $bean = BeanFactory::getBean('CoMO_Orders', $_GET['record']);
    //chec the order type, if it is not a plot (or related order), load quickcreate view.
    if ($bean->order_type == "Repair" || $bean->order_type == "Other" || $bean->order_type == "Stock") {

        header('Location: /index.php?module=CoMO_Orders&action=SmallDetail&record=' . $bean->id);
        exit;

    } else {

        $metadataFile = "custom/modules/CoMO_Orders/metadata/detailviewdefs.php";

        $this->dv = new DetailView2();
        $this->dv->ss = &$this->ss;
        $this->dv->setup($this->module, $this->bean, $metadataFile, get_custom_file_if_exists('include/DetailView/DetailView.tpl'));
    }
}

}

If the record is no a Repair, Other or Stock. It works fine. If it is then it open up the error window and displays the record in there, Please see the screen shot attached.

IS there a better way to load a different view?

@peted
I recommend don’t change function ‘preDisplay’.

  1. Make function ‘getMetaDataFile’ in file …/view.detail.php
/* add */
    public function getMetaDataFile()
    {
        if ($this->bean->order_type == "Repair" || $this->bean->order_type == "Other" || $this->bean->order_type == "Stock") {
            $oldType = $this->type;
            $this->type = $oldType . 'ros';
        }
        $metadataFile = parent::getMetaDataFile();
        if ($this->bean->order_type == "Repair" || $this->bean->order_type == "Other" || $this->bean->order_type == "Stock") {
            $this->type = $oldType;
        }
        
        return $metadataFile;
    }
/* */
  1. Your metadata of detail view should have next format name of file: …/detailrosviewdefs.php . This file will work for “Repair” or “Other” or “Stock” the file ‘detailviewdefs.php’ will work for other.

This example for detailview, but you can do the same thing for editview.

1 Like

Thanks, Not sure if i am missing some thing here. This is what the view.detail.php, I ended up passing the $bean as well, as it was not entering the if statements without it.

<?php

require_once ‘include/MVC/View/views/view.detail.php’;
class CoMO_OrdersViewDetail extends ViewDetail
{
public function getMetaDataFile($bean)
{
if ($bean->order_type == “Repair” || $bean->order_type == “Other” || $bean->order_type == “Stock”) {
$oldType = $this->type;
$this->type = $oldType . ‘ros’;

    }

    $metadataFile = parent::getMetaDataFile();
    if ($bean->order_type == "Repair" || $bean->order_type == "Other" || $bean->order_type == "Stock") {
        $this->type = $oldType;
    }

    return $metadataFile;
}

// function runs before view is disaplyed.
public function preDisplay()
{
    $bean = BeanFactory::getBean('CoMO_Orders', $_GET['record']);
    $metadataFile = CoMO_OrdersViewDetail::getMetaDataFile($bean);
    echo $metadataFile;exit;
    $this->dv = new DetailView2();
    $this->dv->ss = &$this->ss;
    $this->dv->setup($this->module, $this->bean, $metadataFile, get_custom_file_if_exists('include/DetailView/DetailView.tpl'));
}

}

I added this line to the code echo $metadataFile;exit;

and on a stock item, $metadataFile is blank and for other items it is custom/modules/CoMO_Orders/metadata/detailviewdefs.php

What have i done wrong?

I have changed passing $bean to adding this line to the function:

$bean = BeanFactory::getBean('CoMO_Orders', $_GET['record']);

as i noticed this in the error log:

PHP Warning:  Declaration of CoMO_OrdersViewDetail::getMetaDataFile($bean) should be compatible with SugarView::getMetaDataFile() in /var/www/html/custom/modules/CoMO_Orders/views/view.detail.php

This has solved the error message, but the symptoms are exactly the same. Now there is not error in apache or suitecrm.log when calling this view.

@peted

Bean you can find in object $this->bean .

Thanks that has moved thing on a little.

What i have noticed is that after a rebuild if i click a record that uses the ‘ros’ view it, then all records from then on will have the ‘ros’ view.

If i rebuild again i click a record that uses the normal view, then all records will display the normal view until the next rebuild.

IS this to do with cache? can the cache be turned off for these views?

@peted
Do you return the first type as in my code?
I don’t have this problem in my test platform.

I am using your function as you typed it. I have it working on my system now.

I assume that you have developer mode active, meaning no caching?

I have added the flowing lines to clear the theme cache for this module when it loads. and that seems to have fixed it.

    $repair = new RepairAndClear();
    $repair->repairAndClearAll(array('clearThemeCache'), array(translate('CoMO_Orders')), true, false);

So the whole class looks like this:

class CoMO_OrdersViewDetail extends ViewDetail{
public function getMetaDataFile(){

    if ($this->bean->order_type == "Repair" || $this->bean->order_type == "Other" || $this->bean->order_type == "Stock") {

        $oldType = $this->type;

        $this->type = $oldType . 'ros';

    }

    $metadataFile = parent::getMetaDataFile();

    if ($this->bean->order_type == "Repair" || $this->bean->order_type == "Other" || $this->bean->order_type == "Stock") {

        $this->type = $oldType;

    }

    return $metadataFile;

}

public function preDisplay(){

    // find out what meta file is needed

    $metadataFile = CoMO_OrdersViewDetail::getMetaDataFile();

    // Rebuild theme for this moudle to clear the cache, making sure the correct view loads.

    $repair = new RepairAndClear();

    $repair->repairAndClearAll(array('clearThemeCache'), array(translate('CoMO_Orders')), true, false);

    //load view.

    $this->dv = new DetailView2();

    $this->dv->ss = &$this->ss;

    $this->dv->setup($this->module, $this->bean, $metadataFile, get_custom_file_if_exists('include/DetailView/DetailView.tpl'));

}

}

Thanks for you help. I am interested if this is the correct way, as it seems silly to have to clear the cache each load, but for out needs, i doubt it will be too much of an issue.

@peted
Without problems.
Add new form to the function ‘display’ to file …/view.detail.php:

  public function display(){
   if ($this->bean->order_type == "Repair" || $this->bean->order_type == "Other" || $this->bean->order_type == "Stock") {
      $this->dv->formName='DetailView_ros';
    }
    parent::display();
  }
1 Like

That works, Thanks again