Plugin Directory

Changeset 3474817


Ignore:
Timestamp:
03/04/2026 06:09:08 PM (4 weeks ago)
Author:
whiteshadow
Message:

Improve automatic control generation for struct settings and ControlGenerator

For example, when there's only a single control, we don't necessarily need to wrap it in a new section. Also, we can handle struct settings that don't implement PredefinedSet by iterating over their children.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • admin-menu-editor/trunk/customizables/Builders/ElementBuilderFactory.php

    r3400590 r3474817  
    217217        } else if ( $setting instanceof Settings\NumericSetting ) {
    218218            return $this->number($setting);
    219         } else if ( $setting instanceof Settings\PredefinedSet ) {
    220             return $this->autoSection($setting);
     219        } else if ( $setting instanceof Settings\ControlGenerator ) {
     220
     221            $controls = $setting->createControls($this);
     222            if ( count($controls) === 1 ) {
     223                //If there's only one control, just return it directly. We don't need a new section.
     224                return reset($controls);
     225            } else {
     226                //Create a section that contains the controls.
     227                return $this->section($setting->getLabel(), ...$controls)
     228                    ->params(['preferredRole' => Controls\Section::CONTENT_ROLE]);
     229            }
     230
    221231        } else if ( $setting instanceof Settings\WithSchema\SingularSetting ) {
    222232
     
    235245            } else if ( $schema instanceof Schemas\Number ) {
    236246                return $this->number($setting);
    237             } else {
    238                 return $this->textBox($setting);
    239             }
    240 
    241         } else if ( $setting instanceof Settings\AbstractSetting ) {
     247            }
     248        }
     249
     250        //Turn general structs into sections. (Composite settings are excluded because they usually
     251        //need custom handling; their children don't make sense as independent controls.)
     252        if (
     253            ($setting instanceof Settings\AbstractStructSetting)
     254            && !($setting instanceof Settings\CompositeSetting)
     255        ) {
     256            return $this->autoSection($setting);
     257        }
     258
     259        if ( $setting instanceof Settings\AbstractSetting ) {
    242260            switch ($setting->getDataType()) {
    243261                case 'color':
     
    276294
    277295        $firstSetting = reset($settings);
    278         if ( $firstSetting instanceof Settings\PredefinedSet ) {
     296        if ( $firstSetting instanceof Settings\ControlGenerator ) {
    279297            $controls = $firstSetting->createControls($this);
    280298            return $this->section(
     
    282300                ...$controls
    283301            )->params(['preferredRole' => $role]);
     302        }
     303
     304        //Handle simple structs.
     305        if (
     306            (count($settings) === 1)
     307            && ($firstSetting instanceof Settings\AbstractStructSetting)
     308            && !($firstSetting instanceof Settings\CompositeSetting)
     309        ) {
     310            $section = $this->section($title ?: $firstSetting->getLabel())->params(['preferredRole' => $role]);
     311            foreach ($firstSetting as $setting) {
     312                $section->add($this->auto($setting));
     313            }
     314            return $section;
    284315        }
    285316
Note: See TracChangeset for help on using the changeset viewer.