1

I have some options to configure a plugin of mine. These options contain one subform field, that can be repeated. The purpose of the field is to match names of one type to ones of another type. The first type (Field 1) has around 170 names and the second (Field 2) around 500 to choose from.

Also i need to create a default matching value so there is no need matching stuff after installing the plugin.

After i did this the whole browser almost crashed and site needed around two minutes until it was usable. So first i deactivated the fancy-select Layout to make it faster, but it only helped a little.

Here is a piece of the plugin config that is making the trouble:
(don't miss the long default value)

<?xml version="1.0" encoding="utf-8"?>
<extension type="plugin" group="system" method="upgrade">
    ...
    <config>
        <fields name="params" addfieldprefix="Joomla\Plugin\System\MyPlugin\Field">
            <fieldset name="attributes">
                <field name="ma" type="subform" multiple="true" layout="joomla.form.field.subform.repeatable" buttons="add,remove" groupByFieldset="false" label="Infos" hiddenLabel="true" default='{"ma1":{"ha":"SMOKING_NOT_ALLOWED","fa":"NO SMOKING"},"ma2":{"ha":"PETS_ALLOWED","fa":"ANIMALS"},"ma3":{ "ha":"VIEW_OCEAN","fa":"OCEAN VIEW"},"ma4":{"ha":"VIEW_LAKE","fa":"LAKE VIEW"},"ma5":{"ha":"BARRIER_FREE ","fa":"WHEELCHAIR"},"ma6":{"ha":"POOL","fa":"SWIMMING POOL"},"ma7":{"ha":"POOL_PRIVATE","fa": "PRIVPOOL"},"ma8":{"ha":"WHIRLPOOL","fa":"WHIRLPOOL"},"ma9":{"ha":"INTERNET","fa":"ENTINTNET"}," ma10":{"ha":"INTERNET_WIFI","fa":"WLAN"},"ma11":{"ha":"SAUNA","fa":"SAUNA"},"ma12":{"ha ":"FIREPLACE","fa":"FIREPLACE"},"ma13":{"ha":"CHILDFRIENDLY","fa":"CHILD FRIEND"},"ma14":{"ha":"RENOVATED", "fa":"MODERN"},"ma15":{"ha":"LUXURY","fa":"LUXURY"},"ma16":{"ha":"WASHER","fa":"WASHER "},"ma17":{"ha":"DRYER","fa":"DRYER"},"ma18":{"ha":"HEATING","fa":"HEATING"},"ma19" :{"ha":"HEATING_UNDERFLOOR","fa":"BODHEIZUNG"},"ma20":{"ha":"AIR_CONDITION","fa":"KLIMA"},"ma21":{"ha": "VENTILATOR","fa":"VENTILATOR"},"ma22":{"ha":"SOLARIUM","fa":"SOLARIUM"},"ma23":{"ha":"TV","fa ":"TV"},"ma24":{"ha":"TV_CABLE-K","fa":"TV"},"ma25":{"ha":"TV_SATELLITE-S","fa": "TV"},"ma26":{"ha":"TV_DIGITAL-D","fa":"TV"},"ma27":{"ha":"DVD_PLAYER","fa":"DVD"} ,"ma28":{"ha":"CD_PLAYER","fa":"CD"},"ma29":{"ha":"RADIO","fa":"RADIO"},"ma30":{ "ha":"COMPUTER","fa":"PC"},"ma31":{"ha":"TELEPHONE","fa":"TEL"},"ma32":{"ha":"ALARM_HOUSE ","fa":"ALARM"},"ma33":{"ha":"ROOMS_BATHROOM","fa":"BZ"},"ma34":{"ha":"ROOMS_BEDROOM","fa": "SZ"},"ma35":{"ha":"SAFE","fa":"SAFE"},"ma36":{"ha":"BALCONY","fa":"BALKONY"}," ma37":{"ha":"TERRACE","fa":"TERRACE"},"ma38":{"ha":"GARDEN","fa":"GARDEN"},"ma39":{"ha ":"GARDEN_FURNITURE","fa":"GARDEN FURNITURE"},"ma40":{"ha":"POOL_OUTDOOR","fa":"RELAX"},"ma41":{"ha":"GAME_CONSOLE_PS3", "fa":"PLAYSTAT"},"ma42":{"ha":"GAME_CONSOLE_PS4","fa":"PLAYSTAT"},"ma43":{"ha":"PLAYGROUND","fa":"PLAYGROUND "},"ma44":{"ha":"GRILL","fa":"GRILL"},"ma45":{"ha":"PARKING","fa":"ENTPARKP"},"ma46" :{"ha":"PARKING_SPOT","fa":"PARKING PLATZ"},"ma47":{"ha":"BICYCLE_RENTALS_ON_SITE","fa":"FAHRRADNU"},"ma48":{"ha": "KITCHEN","fa":"KUECHE"},"ma49":{"ha":"OVEN","fa":"OVEN"},"ma50":{"ha":"COOKTOP","fa ":"STOVE"},"ma51":{"ha":"MICROWAVE","fa":"MICRO"},"ma52":{"ha":"FRIDGE","fa":"FRIDGE"} ,"ma53":{"ha":"ICE_MAKER","fa":"EISSCHR"},"ma54":{"ha":"DISHWASHER","fa":"SPUELMA"},"ma55":{ "ha":"WATER_HEATER","fa":"KETTLE"},"ma56":{"ha":"COFFEE_MACHINE","fa":"COFFEE"},"ma57":{"ha":"TOASTER ","fa":"TOASTER"},"ma58":{"ha":"MINIBAR","fa":"MINIBAR"},"ma59":{"ha":"KETTLE","fa": "KETTLE"},"ma60":{"ha":"SHOWER","fa":"SHOWER"},"ma61":{"ha":"BATHTUB","fa":"BATHTUB"}," ma62":{"ha":"SEPARATE_TOILET","fa":"WC"},"ma63":{"ha":"VACUUM_CLEANER","fa":"STAUBSAUG"},"ma64":{"ha ":"BIKE_RENTAL","fa":"FAHRRADNU"},"ma65":{"ha":"BOAT_RENTAL","fa":"ENTBOOTV"},"ma66":{"ha":"RESTAURANT", "fa":"ENTRES"},"ma67":{"ha":"MINIMARKET","fa":"ENTSUPERM"},"ma68":{"ha":"FAMILY_FRIENDLY","fa":"FAMILY "},"ma69":{"ha":"WATER_SPORTS","fa":"WATER SPORTS"},"ma70":{"ha":"SKI_VACATION","fa":"SKI"},"ma71" :{"ha":"BEACH_VACATION","fa":"BEACH"},"ma72":{"ha":"WELLNESS","fa":"WELLNESS"},"ma73":{"ha": "GOLF","fa":"GOLF"},"ma74":{"ha":"FISHING","fa":"FISHING"},"ma75":{"ha":"HORSEBACK_RIDING","fa ":"RIDING"},"ma76":{"ha":"WATERPARK","fa":"EINTRWPARK"},"ma77":{"ha":"GOLF_COURT","fa":"ENTGOLF"} ,"ma78":{"ha":"GREEN_PROPERTY","fa":"GREEN_ELECTRICITY"},"ma79":{"ha":"LAKE_FRONT","fa":"LAKE VIEW"},"ma80":{ "ha":"OCEAN_FRONT","fa":"OCEAN_VIEW"},"ma81":{"ha":"RECYCLING_GLASS","fa":"WASTESEP_GLASS"},"ma82":{"ha":"RECYCLING_PAPER ","fa":"WASTESEP_PAPER"},"ma83":{"ha":"RECYCLING_PLASTIC","fa":"WASTESEP_PLASTIC"},"ma84":{"ha":"RECYCLING_ORGANIC","fa": "WASTESEP_ORGANIC"},"ma85":{"ha":"ENERGY_SAVING_WIND_POWER","fa":"WIND_TURBINE"},"ma86":{"ha":"ENERGY_SOLAR_THERMAL_PANELS","fa":"SOLAR_THERMAL"}," ma87":{"ha":"ENERGY_HEAT_PUMPS","fa":"HEAT_PUMPS"},"ma88":{"ha":"ENERGY_GREEN_ELECTRICITY_PROVIDER","fa":"GREEN_ELECTRICITY"},"ma89":{"ha ":"ENERGY_GEOTHERMICS","fa":"GEOTHERMAL_ENERGY"},"ma90":{"ha":"ENERGY_INFRARED_HEATING_PANELS","fa":"INFRARED_HEATING"},"ma91":{"ha":"INSULATION_WALL", "fa":"INSULATED_FACADE"},"ma92":{"ha":"INSULATION_FLOOR","fa":"INSULATED_FLOORS"},"ma93":{"ha":"WATER_SAVING_SHOWER","fa":"WATERSAVING_SHOWERS "},"ma94":{"ha":"WATER_SAVING_TOILET","fa":"WATERSAVING_TOILETS"},"ma95":{"ha":"WATER_SAVING_TAPS","fa":"WATERSAVING_TAPS"},"ma96" :{"ha":"RAINWATER_TREATMENT","fa":"RAINWATER_HARVESTING"},"ma97":{"ha":"OWN_PRODUCE_VEGETABLES","fa":"OWN_VEGETABLES"},"ma98":{"ha": "OWN_PRODUCE_FRUITS","fa":"OWN_FRUIT"},"ma99":{"ha":"OWN_PRODUCE_EGGS","fa":"OWN_EGGS"},"ma100":{"ha":"SHOPPING","fa ":"ENTEIN"},"ma101":{"ha":"SKIING","fa":"SKI"},"ma102":{"ha":"WINERY","fa":"WINERIES"} }'>
                    <form>
                        <fieldset name="section1" label="Section1">
                            <field name="ha" label="Field 1" type="mytypes1" la__yout="joomla.form.field.list-fancy-select" hiddenLabel="true" />
                            <field name="fa" label="Field 2" type="mytypes2" la__yout="joomla.form.field.list-fancy-select" hiddenLabel="true" />
                        </fieldset>
                    </form>
                </field>
            </fieldset>
        </fields>
    </config>
</extension>

Update:

So I forgot one of the custom fields is calling an outside API to get the 500 values. Of course, that should be done only once, but I don't get how a singleton call can be done inside this custom field API call.

Here is the important piece of code

<?php

namespace Joomla\Plugin\System\Ha\Field;

use Joomla\CMS\Uri\Uri;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Http\HttpFactory;
use Joomla\CMS\Form\Field\ListField;

\defined('_JEXEC') or die;

class HaField extends ListField
{
    protected $type = 'Ha';
    protected static $options = [];

    public function getOptions()
    {
        // static use of only once called API is not working
        if (empty($this->options)) {
            $this->options = array(
                'Content-Type' => 'application/json',
            );
            $this->http = HttpFactory::getHttp($this->options);
            $this->base_url = 'https://someservice.com';
            $this->options['api-key'] = 'xxxxxxxxxxxxxx';
            $this->url_params = [];
            $this->url_params['user'] = '12345';

            $url = Uri::getInstance($this->base_url . '/v1/fields');
            $url->setQuery($this->url_params);
            $options = [];

            try {
                $response = $this->http->get($url->toString(), $this->options);
                $results = json_decode($response->getBody());

                foreach ($results as $node) {
                    $options[] = HTMLHelper::_('select.option', $node, $node);
                }

            } catch (RuntimeException $e) {
                $response = null;
            }

            $this->options = array_merge(parent::getOptions(), $options);
        }

        return $this->options;
    }
}
1
  • We do not "resolve" questions via comment. Please either post an answer to your question or if you feel your question is not very valuable to other, you can choose to self-delete the question. May 2, 2023 at 22:33

1 Answer 1

0

Solved it. The static $options = []; cache needed to be taken inside the function. That way the API is only called once.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.