dnadesign / silverstripe-elemental-virtual

Allows Content Blocks to be reused between pages.
BSD 3-Clause "New" or "Revised" License
7 stars 26 forks source link

Template Override doesn't work (Component template isn't used) #26

Closed rafaeldsousa closed 1 year ago

rafaeldsousa commented 5 years ago

When trying to override the component template in local theme, that doesn't work. Going through the code I noted that the templates for this component aren't actually used and it goes straight to ElementHolder.ss in SilverStripe Elemental. Tried including private static $controller_template = 'MyElementHolder'; in VirtualElement as per described in SilverStripe Elemental, but that also did not work.

muppsy007 commented 5 years ago

Hi @rafaeldsousa ,did you even find a solution to this? I'm having the same issue.

rafaeldsousa commented 5 years ago

Hi @muppsy007 , I ended up finding out that trying to override the actual VirtualElement doesn't quite work. VirtualElement seems to use ElementHolder as template, so I've customised that one to actually make things work for me.

I ended up with this for my elemental ElementHolder

<% if ClassName == DNADesign\ElementalVirtual\Model\ElementVirtual %>
  <div class="element virtual $SimpleClassName.LowerCase<% if $LinkedElement.StyleVariant %> $LinkedElement.StyleVariant<% else_if $StyleVariant %> $StyleVariant<% end_if %><% if $ExtraClass %> $ExtraClass<% end_if %>" id="$Anchor">
    $Element
  </div>
<% else %>
<div class="element $getSimpleClassName.LowerCase <% if $StyleVariant %>$StyleVariant<% end_if %><% if $ExtraClass %> $ExtraClass<% end_if %>" id="$Anchor">
    $Element
</div>
<% end_if %>

and this for my VirtualElementExtension

use SilverStripe\ORM\DataExtension;
use SilverStripe\Forms\FieldList;

class ElementVirtualExtension extends DataExtension
{
  public function SimpleClassName()
  {
    return $this->owner->LinkedElement()->getSimpleClassName();
  }

  public function updateCMSFields(FieldList $fields)
  {
    $fields->removeByName('TitleAndDisplayed');

    return $fields;
  }

  /**
   * This can be overridden on child elements to create a summary for display
   * in GridFields.
   *
   * @return string
   */
  public function Summary()
  {
    $id = $this->owner->LinkedElement->ID;
    $type = $this->owner->LinkedElement->getType();
    $title = $this->owner->LinkedElement->Title;

    $summary = $type . ($title ? ' - ' . $title : '')  . ' (#' . $id . ')';

    return $summary;
  }
}

This worked for my case, but I've seen a few more issues related to virtualising Lists, which I'm not entirely sure how that got resolved.

Hope this helps you.

muppsy007 commented 5 years ago

Thanks very much @rafaeldsousa. I started doing something similar with ClassName in the template. Good to know that is not wasted. :-)

Side question: Did you also extend BaseElement from elemental as well as this VirtualElement. I'm having major issues with doing so: https://github.com/dnadesign/silverstripe-elemental-virtual/issues/29

Thought you might have encountered similar if you Extended ElementVirtual.

rafaeldsousa commented 5 years ago

[Emergency] Uncaught BadMethodCallException: Object->__call(): the method 'getLayout' does not exist on 'ElementGallery' I think its the same as what I had above, if you look I have both getSimpleClassName and SimpleClassName both doing the same thing, but if it's a virtual element, it doesn't like get so I had to recreate the function in my VirtualElementExtension but without the get. Try that with your getLayout.

wilr commented 1 year ago

I've corrected the missing controller template now https://github.com/dnadesign/silverstripe-elemental-virtual/commit/bcdc2d0db8646190f64973e24cb83294d0bf592b