Make Path Formatter work; Customized Code


#1

There is a nice example for formatting object properties ==>
https://pimcore.com/docs/5.0.x/Objects/Object_Classes/Class_Settings/Path_Formatter.html
In this example you have to define “\Website\TheFormatter” as function doing the formatting.

But I can’t figure out the right directory where to put this code on the server.

Can anybody give me a hint for that?

Thanks


#2

Hi,

in the case of that example you should put it in:
src/Website/TheFormatter.php

Cheers, Igor


#3

Many thanks! Simple and effective
Just one example of critical information missing in the documentation.


#4

Hi,
this information is somehow Symfony, Composer and PHP basics :wink: .
But we are always happy about documentation improvement and pull requests are welcome.

Thank you very much…


#5

Hi,

The example is working very well with a one-to-many relationship. But I can not get it to work with a many-to-many object relationship. If I convert it back to one-to-many it does work. I tried manipulation the return array but nothing seems to work, it has no effect at all.
Can somebody point me in the right direction please?
(pimcore version 5.6.0)
Thank you in advance!

EDIT: It seems to work fine with a “many-to-many relation”, not with a “many-to-many object relation”. I can’t find in the docs what the difference between the two is. I would be grateful if somebody could clarify.


#6

Could you share your code? My many-to-many formatter looks and works the same as for one-to-many.


#7

Thank you for the quick reply. It has something to do with the fact that the relation in its turn has a relation with another object.

I am trying this out at the moment:

public static function formatPath($result, ElementInterface $source, $targets, $params) {
dump($targets);
 foreach ($targets as $key => $item) {
   $result[$key]["CharacterisitcProperty"] = $item["CharacterisitcProperty"];
   $newPath = basename($item["CharacterisitcProperty"]["path"]);
     
   // don't forget to use the same key, otherwise the matching doesn't work
   //$result[$key]["CharacterisitcProperty"]["path"] = $newPath;
   $result[$key]["CharacterisitcProperty"]= $newPath;
 } 
 dump($result);
 return $result;

}


#8

Check this code below with multi relation object formatter, how I do it:

public static function formatPath($result, ElementInterface $source, $targets, $params) {
        foreach ($targets as $key => $item) {            
            if ($item["type"] == "object") {
                $targetObject = Concrete::getById($item["id"]);
                $newPath = $targetObject->getDrugId()." - ".$targetObject->getDrugName();
             }                
            // don't forget to use the same key, otherwise the matching doesn't work
            $result[$key]= $newPath;
        }
        return $result;
    }

#9

Thank you again. I’ll play around with it and see if I can get it to work.
– EDIT –
If I understand it right I have one more level of nesting in my case and I fail to build up the $result array in a decent way. Here is a dump of the $targets array. I would like to change the “path” in “CharacterisitcProperty”.

array:2 [▼
13795 => array:17 [▼
"id" => 13795
"fullpath" => "/Q8/typicals/characteristics/5559"
"type" => "object"
"subtype" => "object"
"filename" => "5559"
"creationDate" => 1548669686
"modificationDate" => 1548669697
"published" => true
"classname" => "Q8Characteristic"
"idPath" => "/1/12555/12940/13789/13795"
"inheritedFields" => array:4 [▶]
"permissions" => array:15 [▶]
"locked" => false
"CharacteristicValue" => "200"
"CharacterisitcProperty" => array:5 [▼
  "id" => 13245
  "path" => "/Q8/typicals/properties/Flash Point, COC"
  "subtype" => "object"
  "type" => "object"
  "published" => true
]
"Unit" => array:5 [▶]
"Method" => array:5 [▶]
]
13798 => array:17 [▶]
]

– EDIT – (Sorry, limited to 3 replies at the moment and 1 image)


“Characteristic” on its turn has a N:1 relation edit with another class (“Property”)

Hope this helps, thanks.


#10

Oh ok, so are you using many-to-many OBJECT relation? Could you also pls share the screenshot of these class definitions?


#11

I looked into the code and I am not sure if this is even possible. It seems to match the key of the item and set the path property to it (in your case item 13795) and not to subitem = CharacterisitcProperty.