Coder Social home page Coder Social logo

hdpero / sortablefile Goto Github PK

View Code? Open in Web Editor NEW

This project forked from bummzack/sortablefile

0.0 2.0 0.0 1.73 MB

An extension for SilverStripe 4.1+ that allows sorting of multiple attached images (extends UploadField)

License: BSD 3-Clause "New" or "Revised" License

PHP 60.48% JavaScript 33.37% CSS 6.15%

sortablefile's Introduction

Sortable UploadField

Scrutinizer Code Quality Code Coverage Build Status Latest Stable Version Latest Unstable Version Monthly Downloads

An extension for SilverStripe 4.1+ that allows sorting of files attached via UploadField.

This module decorates the existing UploadField and adds sorting capabilities to it. This is meant to be used with a many_many relation of Files or Images.

screen-capture

Installation

This module only works with SilverStripe 4.1+.

For a version compatible with SilverStripe 3, please use a 1.x release.

The easiest way is to use composer:

composer require bummzack/sortablefile ^2.0

Run dev/build afterwards.

Usage

Usage is pretty simple. Just use SortableUploadField instead of UploadField to manage your many_many relations. To persist the sort-order, an additional extra-field with the sort-order has to be added to the many_many relation. You can do so by specifying the sort column via many_many_extraFields (see example below).

By default the SortableUploadField assumes that the sort-column is named SortOrder. If you want to use another field-name (for example Sort), you have to explicitly set it:

SortableUploadField::create('Files')->setSortColumn('Sort');

Example setup for many_many

Let's assume we have a PortfolioPage that has multiple Images attached.

The PortfolioPage looks like this:

use SilverStripe\Assets\Image;
use SilverStripe\Forms\FieldList;
use Bummzack\SortableFile\Forms\SortableUploadField;

class PortfolioPage extends Page
{   
    // This page can have many images
    private static $many_many = [
        'Images' => Image::class
    ];
    
    // this adds the SortOrder field to the relation table. 
    // Please note that the key (in this case 'Images') 
    // has to be the same key as in the $many_many definition!
    private static $many_many_extraFields = [
        'Images' => ['SortOrder' => 'Int']
    ];

    public function getCMSFields()
    {
        $this->beforeUpdateCMSFields(function (FieldList $fields) {
            $fields->addFieldToTab('Root.Main', SortableUploadField::create(
                'Images', $this->fieldLabel('Images')
            ));
        });
        
        return parent::getCMSFields();
    }
}

Once this has been set up like described above, then you should be able to add images in the CMS and sort them by dragging them (use the handle on the left).

Templates

Sorting the Files via a relation table isn't easily achievable via a DataExtension. This is why it's currently up to the user to implement a getter that will return the sorted files, something along the lines of:

// Use this in your templates to get the correctly sorted images
public function SortedImages()
{
    return $this->Images()->Sort('SortOrder');
}

And then in your templates use:

<% loop $SortedImages %>
  $ScaleWidth(500)
<% end_loop %>

Alternatively, you could simply use the sort statement in your template, which will remove the need for a special getter method in your page class.

<% loop $Images.Sort('SortOrder') %>
  $ScaleWidth(500)
<% end_loop %>

What happened to has_many support?

Support for has_many relations has been dropped, since it can lead to a very bad user experience if a file can only be added to a single page. Imagine a user added an image to Page A, then adds the same image via Add from files to Page B. The file would then be removed from Page A, without any warning or explanation, which is bad UX.

sortablefile's People

Contributors

bummzack avatar patricknelson avatar dospuntocero avatar kinglozzer avatar ntd avatar zauberfisch avatar 3dgoo avatar mlewis-everley avatar sheadawson avatar g4b0 avatar

Watchers

James Cloos avatar Petar Simic avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.