GrafikMatthew GrafikMatthew - 1 month ago 20
PHP Question

Pimcore: Retrieving Documents or Pages from Editable Links

I am trying to retrieve the Document Object or Page Object from an editable link in Pimcore. As per the documentation, I have access to the following methods:


  • getHref()
    Get the path of this link as
    STRING

  • getText()
    Get the text of the link as
    STRING

  • getTarget()
    Get the target of the link as
    STRING

  • getParameters()
    Get the query params of the link as
    STRING

  • getAnchor()
    Get the anchor text of the link as
    STRING

  • getTitle()
    Get the title of the link as
    STRING

  • getRel()
    Get the rel text of the link as
    STRING

  • getTabindex()
    Get the tabindex of the link as
    STRING

  • getAccessKey()
    Get the access key of the link as
    STRING

  • isEmpty()
    Whether the editable is empty or not
    BOOL



The ZEND PHP:

<?php if( !empty( $this->block( "topfive_linkblock" ) ) ) : ?>
<ul class="wrapper">
<?php while( $this->block( "topfive_linkblock" )->loop() ) : ?>
<li><?= $this->link( "topfive_links" ); ?></li>
<?php endwhile; ?>
</ul>
<?php endif; ?>


While this does well to create anchor links, I'd like to be able to get the Custom Properties for these pages so I can pull things over like preview images and other bits of information.

With WordPress, you can get posts by their URL, though I could not find a similar function in Pimcore/Zend.

Additional Info

If I use the undocumented method
getDataForResource()
I can get an Array with what I need, but I don't want to access the key as an array if that's no the appropriate way to do this (in case architecture changes alter the behavior in the future or remove this method).

<?= print_r( $this->link( "topfive_links" )->getDataForResource(), true ); ?>


Outputs:

Array
(
[text] => Associations
[path] => /customers/associations
[target] =>
[parameters] =>
[anchor] =>
[title] =>
[accesskey] =>
[rel] =>
[tabindex] =>
[class] =>
[attributes] =>
[internal] => 1
[internalId] => 49
[internalType] => document
[type] => internal
)


The number in the
internalId
key is what I'm aiming for. My question is, what's the appropriate and most efficient way this can be done? Can I simply access this value from this array safely?

Accepted Solution:

Igor's solution works in both respects. We ended up using the HREF editable instead of the LINK editable.

Answer

You can get all the data about the element in link editable this way:

<?php
/** @var \Pimcore\Model\Document\Tag\Link $link */
$link = $this->link( "topfive_links" );
?>

<?php if ($this->editmode): ?>

    <?php if( !empty( $this->block( "topfive_linkblock" ) ) ) : ?>
        <ul class="wrapper">
            <?php while( $this->block( "topfive_linkblock" )->loop() ) : ?>
                <li><?= $link ?></li>
            <?php endwhile; ?>
        </ul>
    <?php endif; ?>

<?php else: ?>

    <?php
    while( $this->block( "topfive_linkblock" )->loop() ) {
        $link = $this->link( "topfive_links" );
        $linkElementData = $link->getData();

        $element = null;
        if ($linkElementData["internal"]) {
            $elementId = $linkElementData["internalId"];
            switch ($linkElementData["internalType"]) {
                case "document":
                    $element = \Pimcore\Model\Document::getById($elementId);
                    break;

                case "asset":
                    $element = \Pimcore\Model\Asset::getById($elementId);
                    break;
            }
        }

        if ($element) {
            echo get_class($element) . "<br>";
        }
    } 
    ?>

<?php endif ?>

For your use case the href editable might be a better choice: https://www.pimcore.org/docs/latest/Documents/Editables/Href.html

With href editable you can just use getElement method.