Zauberfisch / silverstripe-easy-linkfield

SilverStripe inline link field that allows adding one or multiple links to any object and saves into a single DB field
BSD 3-Clause "New" or "Revised" License
2 stars 0 forks source link

Using File option doesn't work #2

Closed BradyEdgar94 closed 3 years ago

BradyEdgar94 commented 3 years ago

Everything works great except the file part. I chose an existing file as the instructions, it looks fine in the backend. However, when I view the frontend it breaks with a [Notice] Undefined index: type error.

I debugged it a bit and learned that anytime I add $Link or $File to the frontend, it shows that error. When I also try to use $FileID.URL it's empty.

So something is off with the File part of this that is breaking the frontend.

Here is the error message for reference:

Screen Shot 2021-06-22 at 8 33 28 PM
Zauberfisch commented 3 years ago

@BradyEdgar94 I was not able to reproduce your particular error, but I did find a mistake in the FileLink class. I've fixed that now.

Please update to the latest version and in the Template use $Link or $File.URL (not $FileID.URL).
Please also note, that at the moment it will throw an error if you call a field that doesn't exist. I might change that in the future, but for now, you have to check if the link type is a file and then call $File.URL, otherwise you would call $File on other Link Types aswell such as InternalLink which does not have a file. So your template should look something like this:

<% loop $Buttons.getValue %>
    <%-- Always available Variables: $Link, $AbsoluteLink, $LinkType, $Title, $NewTab --%>
    <%-- And depending on the type: $Page (internal), $PageID (internal), $URL (external), $File (file), $FileID (file), $Email (email), $CountryPrefix (phone), $Number (phone), $PhoneNumber (phone) --%>
    <%-- If you use fields depending on the type, you have to check for the type first, otherwise you will get an error that the field was not found --%>
    <%-- For example <% if $LinkType == 'internal' %>The Link is $Link and the PAGE URLSegment is $Page.URLSegment<% end_if %> --%>
    <% if $LinkType == 'file' %>
        This is File $File.ID ($File.Name) with the URL $Link (or you could use $File.URL)
    <% else %>
        <a href="$Link" <% if $NewTab %>target="_blank"<% end_if %>>$Title</a>
    <% end_if %>
<% end_loop %>
BradyEdgar94 commented 3 years ago

Thanks for looking into this.

So I upgraded but still getting the same issue. I tried uploading a PDF and a PNG but both have the same issue.

Screen Shot 2021-06-22 at 10 33 18 PM

I then tried the code you provided above but same issues. Its any time I use $Link or $File it throws an error:

Screen Shot 2021-06-22 at 8 33 28 PM

Not sure why I would be getting this but I can't access that $File object unfortunately.

My SS version is 4.6.1@stable, PHP 7.4. Is there a specific environment I maybe should try?

BradyEdgar94 commented 3 years ago

Working :)

I updated the version and ran a composer update and everything looks great now.

Thanks for the help!

Zauberfisch commented 3 years ago

Glad it's working now. I'm going to close this issue. Feel free to reopen or create a new one if you have any more issues.