How to Order Relationships in PDF Template

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • czcpf
    Senior Member
    • Aug 2022
    • 160

    How to Order Relationships in PDF Template

    Suppose we have the following code in PDF Template. How do we control the order of radiationMachines? The PDF Template entity in my case is RadiationMachineLocation.



    {{#each radiationMachines}}
    {{/each}}

    What I've tried:

    Custom/Resources/metadata/clientDefs/RadiationMachineLocation.json

    Code:
    "relationshipPanels": {
    "radiationMachines": {
    "filterList": [
    "all"
    ],
    "orderBy": "machineStatusInUseFirst",
    "orderDirection": "desc"
    }
    }
    Custom/Resources/metadata/entityDefs/RadiationMachine.json

    Code:
    "collection": {
    "orderBy": "machineStatusInUseFirst",
    "order": "desc"
    },​
    Here machineStatusInUseFirst is a custom Orderer class I created and it works in other areas of frontend just fine when viewing list of radiationMachines.
  • czcpf
    Senior Member
    • Aug 2022
    • 160

    #2
    Some other questions regarding this:

    How do you apply a select filter to a relation in PDF template ?

    Since I could not get the above to work, I ended up writing my own Custom/Classes/Pdf/RadiationMachineLocation/CustomDataLoader.php

    and also setting Custom/Resources/pdfDefs/RadiationMachineLocation.json as shown below.

    It seems like alot of effort to do this for PDF template and I would also like to know a better approach since looping through like this is time consuming. Is there a shorthand to return the collection which includes the RAW fields as well. For this particular template, I needed {field)_RAW for one of my fields and I had to set it manually like shown below. When looping through a relation the standard way we have access to {field}_RAW somehow. I realize this database call is retrieving only the raw values in ORM so what is a way to return the collection the same way ESPO would where we have all the translated display fields and the {field_RAW} values in the array?



    Custom/Resources/pdfDefs/RadiationMachineLocation.json
    Code:
    {
    "dataLoaderClassNameList": [
    "__APPEND__",
    "Espo\\Custom\\Classes\\Pdf\\RadiationMachineLocation\\CustomDataLoader"
    ]
    }​
    Custom/Classes/Pdf/RadiationMachineLocation/CustomDataLoader.php

    PHP Code:
    <?php
    
    namespace Espo\Custom\Classes\Pdf\RadiationMachineLocation;
    
    use Espo\Custom\Entities\RadiationMachine;
    use Espo\Custom\Entities\RadiationMachineLocation;
    use Espo\Tools\Pdf\Data\DataLoader;
    use Espo\Tools\Pdf\Params;
    use Espo\ORM\Entity;
    use Espo\ORM\Collection;
    use Espo\ORM\Query\Part\Order;
    
    use Espo\Core\{
    Di,
    Utils\DateTime as DateTimeUtil
    };
    
    use stdClass;
    
    /**
    * @internal This class should not be removed as it's used by custom entities.
    * @implements DataLoader<RadiationMachineLocation>
    */
    
    class CustomDataLoader implements DataLoader,
    
    Di\EntityManagerAware,
    Di\LanguageAware
    
    {
    use Di\EntityManagerSetter;
    use Di\LanguageSetter;
    
    public function load(Entity $entity, Params $params): stdClass
    {
    
    /**
    * @var Collection<RadiationMachine> $radiationMachineCollection
    */
    $radiationMachineCollection = $this->entityManager
    ->getRDBRepository($entity->getEntityType())
    ->getRelation($entity, 'radiationMachines')
    ->where([
    'machineStatus' => ['IN_USE', 'STORED_INOPERABLE']
    ])
    ->order('LIST:machineStatus:IN_USE,STORED_INOPERABLE,TRANSFERRED_SOLD,DISPOSED')
    ->find();
    
    
    $radiationMachinesOrderedByStatus = [];
    
    /**
    * @var RadiationMachine $radiationMachine
    */
    foreach( $radiationMachineCollection as $radiationMachine) {
    $rawValues = (array)$radiationMachine->getValueMap();
    $rawValues['machineStatus'] = $this->language
    ->translateOption($radiationMachine->get('machineStatus'),'machineStatus','RadiationMachine');
    $rawValues['machineStatus_RAW'] = $radiationMachine->get('machineStatus');
    $radiationMachinesOrderedByStatus[] = $rawValues;
    
    }
    
    return (object)[
    'radiationMachinesOrderedByStatus' => $radiationMachinesOrderedByStatus,
    ];
    }
    }

    PSEUDO Template code
    HTML Code:
    <!-- {{#each radiationMachinesOrderedByStatus}} -->
    <p>{{name}}<p>
    <p>{{machineStatusWithColor machineStatus raw=machineStatus_RAW}}</p>
    <!-- {{/each}} -->

    Comment

    • esforim
      Active Community Member
      • Jan 2020
      • 2204

      #3
      I was thinking, maybe there is a "sort" or "order" formula but there doesn't seem to be one.

      Comment

      • czcpf
        Senior Member
        • Aug 2022
        • 160

        #4
        I don’t quite understand why entitydefs collection OrderBy isn’t being invoked for PDF template.

        Comment

        Working...