Jump to:

5542 Posts in 1739 Topics by 1225 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Create another inline button in modeladmin edit form

Moderators: martimiz, Sean, biapar, Willr, Ingo, swaiba, simon_w

Page: 1
Go to End
Author Topic: 176 Views
  • thomas.paulson
    Avatar
    Community Member
    75 Posts

    Create another inline button in modeladmin edit form Link to this post

    I recently created product modeladmin as below, what i want is to show another inline button like 'Update URL' next to category dropdown on change event, please see the attached screenshot

    class Product extends DataObject {

       private static $db = array(
          'URLSegment' => 'Varchar(100)',
          'Title' => 'Varchar(255)',
          'MetaTitle' => 'Varchar(255)',
          'MetaDescription' => 'Varchar(255)',
          'Content' => 'HTMLText',      
          "SortOrder" => "Int",      
          "Size" => "Text",
       );
       

       private static $defaults = array(
          'Title'=>'New Item',
          'URLSegment' => 'new-item'
       );
       
       private static $has_one = array(
          'ProductCategory' => 'ProductCategory',
          'Image1' => 'Image',
          'Image2' => 'Image',
          'Image3' => 'Image'
       );

       //The class of the page which will list this DataObject
       //private static $listing_page_class = 'ProductCategory';
        //Class Naming (optional but reccomended)
       private static $singular_name = "Product";
       private static $plural_name = "Products";
          
       private static $default_sort = '"Title" ASC';

       static $summary_fields = array(
          "ProductThumbnail" => "Image",      
          "Title" => "Name",
          "Size" => "Size",
          'ProductCategory.Title' => 'Category'
       );

       /*
       static $searchable_fields = array(
          'Title',
          'Size',
          'Content'=> array('title' => 'Content'),
          'ProductCategoryID' => array('title' => 'Product Category')
       );
       */
       
       static $field_labels = array(
          'Title' => 'Product Name'
       );

       
       public function getCMSFields()
       {
           $fields = parent::getCMSFields();
          
          / /$fields->addFieldToTab('Root.Main', new TextField('Title','Name'));      

          //Remove Scafolded fields
          $fields->removeFieldFromTab('Root.Main', 'URLSegment');
          $fields->removeFieldFromTab('Root.Main', 'MetaTitle');
          $fields->removeFieldFromTab('Root.Main', 'MetaDescription');

          
          $fields->addFieldToTab('Root.Main', new TextField('Title'));   

          if($this->ID)
           {
             $urlsegment = new SiteTreeURLSegmentField("URLSegment", $this->fieldLabel('URLSegment'));
             //$urlsegment->setURLPrefix($prefix);
             
             $helpText = _t('SiteTreeURLSegmentField.HelpChars', ' Special characters are automatically converted or removed.');
              $urlsegment->setHelpText($helpText);
             $fields->addFieldToTab('Root.Main', $urlsegment);
           }

          $fields->addFieldToTab('Root.Main', new HTMLEditorField('Content'));   

           $fields->addFieldToTab('Root.Main',new ToggleCompositeField('Metadata', 'Metadata',
             array(
                new TextField("MetaTitle", $this->fieldLabel('MetaTitle')),
                new TextareaField("MetaDescription", $this->fieldLabel('MetaDescription'))
             )
          ));
          
          
          
          $fields->removeFieldFromTab("Root.Main","ProductCategoryID");
          
          
          $fields->addFieldsToTab('Root.Main', array(
              DropdownField::create('ProductCategoryID', _t("Product.CATEGORY", "Category"), $this->categoryoptions())
                ,
          ), 'Content');      
          
          
          $fields->addFieldToTab('Root.ProductImages', $uploadField = new UploadField('Image1','Main Product Image'));
          $uploadField->setFolderName('Products/'.$this->URLSegment);
           $fields->addFieldToTab('Root.ProductImages', $uploadField = new UploadField('Image2','Second Product Image'));
          $uploadField->setFolderName('Products/'.$this->URLSegment);
          $fields->addFieldToTab('Root.ProductImages', $uploadField = new UploadField('Image3','Third Product Image'));
          $uploadField->setFolderName('Products/'.$this->URLSegment);
          //$uploadField->setConfig('fileEditFields', 'priorityField');
          //$uploadField->FolderName = 'Uploads/'.'member-photos/'.($this->Title);
          $fields->addFieldToTab('Root.Main', new TextField('Size'));   

          $fields->removeFieldsFromTab('Root.Main', array('SortOrder'));
          
          return $fields;
       }
       
       
          
       public function getProductThumbnail() {
          if ($Image = $this->Image1()->ID) {
             return $this->Image1()->SetWidth(100);
          } else {
             return '(No Image)';
          }
       }   
       /**
        * Helper function for generating list of categories to select from.
        * @return array categories
        */
       private function categoryoptions() {
          $categories = ProductCategory::get()->map('ID', 'NestedTitle')->toArray();
          $categories = array(
             0 => _t("SiteTree.PARENTTYPE_ROOT", "Top-level page")
          ) + $categories;      

          return $categories;
       }

    }

    Attached Files
    176 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.