![]() |
|
csDoctrineActAsAttachablePlugin - 1.0.1Attach custom and preset filetypes to your modules |
|
This behavior permits to attach files to Doctrine objects.
Supported filetypes are the following:
The type "other" represents the base filetype, and is not validated by mimetype.
Others can be set accordingly.
add the following code to your module's schema file:
MyModel:
actAs: [Attachable]
To use csAttachable module, add csAttachable to your settings.yml under "enabled_modules" and add the following code to your template:
<?php include_component('csAttachable', 'attachments', array('form' => $form)) ?>
where $this->form represents the Doctrine Form for whatever class is acting as attachable. It is important to note the plugin does not work if the passed $this->form does not carry an object already saved to the database. For this reason, the component should not be included unless the form object exists, for example:
<?php if ($form->getObject()->getId()): ?> <?php include_component('csAttachable', 'attachments', array('form' => $form)) ?> <?php endif; ?>
Also supported is the csAttachable attachments_list component, which receives an object variable and outputs a UL of attachments and their links. This is handy for frontend development / archive reasons, and also serves as an example for how attachments should be accessed / displayed. Use the following code to access this component:
<?php include_component('csAttachable', 'attachments_list', array('object' => $object)) ?>
Where $object is an item acting as Attachable
After completing the steps above, you will want to override the _form.php partial in your backend module.
Copy all of the code from the same generated module in your project cache. Then, add the form code above after the tag.
At the end, your _form.php partial should look something like this:
<?php include_stylesheets_for_form($form) ?> <?php include_javascripts_for_form($form) ?> <div class="sf_admin_form"> <?php echo form_tag_for($form, '@mymodel') ?> <?php echo $form->renderHiddenFields() ?> <?php if ($form->hasGlobalErrors()): ?> <?php echo $form->renderGlobalErrors() ?> <?php endif; ?> <?php foreach ($configuration->getFormFields($form, $form->isNew() ? 'new' : 'edit') as $fieldset => $fields): ?> <?php include_partial('mymodel/form_fieldset', array('mymodel' => $mymodel, 'form' => $form, 'fields' => $fields, 'fieldset' => $fieldset)) ?> <?php endforeach; ?> <?php include_partial('mymodel/form_actions', array('mymodel' => $mymodel, 'form' => $form, 'configuration' => $configuration, 'helper' => $helper)) ?> </form> <?php if ($form->getObject()->getId()): ?> <?php include_component('csAttachable', 'attachments', array('form' => $form)) ?> <?php endif; ?> </div>
Make sure your upload directory has proper permissions set. If you need to debug, unhide the iframe to see the errors being thrown.
Also, be sure to publish your assets ./symfony plugin:publish-assets to pull in the default attachable styles
Using the included partial, Attachment objects are hashed and placed in uploads/(model)/(file) where (model) is the model acting as Attachable. The Attachment object is then assigned the previous filename to it's [name] property.
Array accessors are not yet available for ActAsAttachable. For example, $model['Attachments'] will return an error.