How to extend - class XenForo_ViewPublic_Attachment_View

AndyB

Well-known member
I'm creating a new add-on and I can't get Listener.php to echo a test string. I expect the word "test" to be echo'ed when I view a thread with attachments.

The file I'm trying to extend is:

/library/Xenforo/ViewPublic/Attachment/View.php

When I create a new Code Event Listener, should I listen to load_class or load_class_view ??

Thank you for your help.
 
Hi Chris,

I've tried listening to load_class_view, but not able to verify if the listener is working.

My folders and file structure

library
--Andy
----OpenPDF
------Lisener.php

Contents of Listener.php

PHP:
<?php

class Andy_OpenPDF_Listener
{
   public static function View($class, array &$extend)
   {
     echo 'test';
     $extend[] = 'Andy_OpenPDF_ViewPublic_Attachment_View';
   }
}

?>

Code Event Listener

pic001.webp
 
Thank you, Chris. That was the problem.

I added the "die;" code to verify Listener.php file is working. With the add-on active attachments are broken.

PHP:
<?php

class Andy_OpenPDF_Listener
{
   public static function View($class, array &$extend)
   {
     die;
     $extend[] = 'Andy_OpenPDF_ViewPublic_Attachment_View';
   }
}

?>
 
Last edited:
I added the following folders and file:

library
--Andy
----OpenPDF
------Lisener.php
------ViewPublic
--------Attachment
----------View.php

The contents of View.php

PHP:
<?php

class Andy_OpenPDF_ViewPublic_Attachment_View extends XFCP_Andy_OpenPDF_ViewPublic_Attachment_View
{
   public function actionOpenPDF()
   {
     $imageTypes = array(
     'gif' => 'image/gif',
     'jpg' => 'image/jpeg',
     'jpeg' => 'image/jpeg',
     'jpe' => 'image/jpeg',
     'pdf' => 'application/pdf',
     'png' => 'image/png'
     );
   }  
}

?>

Notice I added the following to the $imageTypes array:

'pdf' => 'application/pdf',

The above code does not work. What is the correct way to modify the $imageTypes array ??
 
Thank you for your reply, Marcus. Unfortunately I don't know what working with "parent::" means.

Also what is the best way to verify in View.php is working? Is the code line

public function actionOpenPDF()

correct?
 
I have verified that View.php is being called using the "die;" code as shown here:

PHP:
<?php
die;
class Andy_OpenPDF_ViewPublic_Attachment_View extends XFCP_Andy_OpenPDF_ViewPublic_Attachment_View
{
   public function actionOpenPDF()
   {
     // update $imageTypes array
   }   
}

?>
 
However the "die;" doesn't do anything if I put it here:

PHP:
<?php

class Andy_OpenPDF_ViewPublic_Attachment_View extends XFCP_Andy_OpenPDF_ViewPublic_Attachment_View
{
   public function actionOpenPDF()
   {
     die;
   }   
}

?>

This leads me to think the following code is incorrect:

public function actionOpenPDF()
 
You extend this:
PHP:
class XenForo_ViewPublic_Attachment_View extends XenForo_ViewPublic_Base
{
   public function renderRaw()
   {
     $attachment = $this->_params['attachment'];

     $extension = XenForo_Helper_File::getFileExtension($attachment['filename']);
     $imageTypes = array(
       'gif' => 'image/gif',
       'jpg' => 'image/jpeg',
       'jpeg' => 'image/jpeg',
       'jpe' => 'image/jpeg',
       'png' => 'image/png'
     );

     if (in_array($extension, array_keys($imageTypes)))
     {
       $this->_response->setHeader('Content-type', $imageTypes[$extension], true);
       $this->setDownloadFileName($attachment['filename'], true);
     }
     else
     {
       $this->_response->setHeader('Content-type', 'application/octet-stream', true);
       $this->setDownloadFileName($attachment['filename']);
     }

     $this->_response->setHeader('ETag', $attachment['attach_date'], true);
     $this->_response->setHeader('Content-Length', $attachment['file_size'], true);
     $this->_response->setHeader('X-Content-Type-Options', 'nosniff');

     return new XenForo_FileOutput($this->_params['attachmentFile']);
   }
}
you can only hook in from the beginning or at the end of a function. I would output the content of "new XenForo_FileOutput($this->_params['attachmentFile']);" and then try to modify it if it is a pdf file.
 
Hi Marcus,

Thank you so much for the code. I updated my View.php file with the code you show in post #11. Normal attached images still show fine, but when I click a PDF attachment, I get the following error:

Fatal error: Cannot redeclare class XenForo_ViewPublic_Attachment_View in /home/*/www/forums/library/Andy/OpenPDF/ViewPublic/Attachment/View.php on line 35
 
In my View.php file, I can't get past the public function actionOpenPDF()

PHP:
<?php

class Andy_OpenPDF_ViewPublic_Attachment_View extends XFCP_Andy_OpenPDF_ViewPublic_Attachment_View
{
   public function actionOpenPDF()
   {
     // can't seem to get to this point
     setcookie("test", "test", time()+3600*24*365*1, "/");
   } 
}

?>
 
Last edited:
The same as XenForo:
PHP:
return new XenForo_FileOutput($this->_params['attachmentFile']);
 
I'm sorry, Jeremy. I was editing my post #14 while you were replying to the original question.

What I don't understand is why doesn't the code run past public function actionOpenPDF() ?? A cookie should be set if the code runs that function.
 
Last edited:
is actionOpenPDF a function in the original view class? Or are you completely adding this function?
 
If you are adding a function, you'll need to modify what happens when you click on a pdf to go to your function. You'll need to reroute within renderRaw.
 
Top Bottom