How can I change the position of an extra field in a form?

My current Dolibarr Version is 20.0.4

I have already created an extra field which is going to be used in the Vendor proposal’s document.

The extra field works like a charm. Now I wonder if there’s a way to move the extra field on screen to be the first one in the left column, just above the “Discounts” field (Descuentos in my Screenshot).

What I have tried:

Changing the “Position” value, (“Puesto” in my Screenshot) from 100 to 1.

Any help is appreciated.

Can’t be done!

Yeah, that was my assumption too, first all the regular fields, then the extra fields. What if the position number is negative?

No. The negative number didn’t work.

I managed to trick it with some CSS, in the next sense:

[class^=trextrafields_collapse] {
  position: absolute;
  top: 258px;
}

.fichehalfleft {
  margin-top: 40px !important;
}

span.select2.select2-container.select2-container--default {
  text-align: initial;
  width: 180px !important;
  left: 40px;
  margin-right: 40px;
}

[id^=supplier_proposal_extras_prov2_] {
  padding-left: 40px;
}

The custom CSS code is placed in the Environment - CSS Style text area.

It -somehow- works and I feel fine now, but it is going to be hard to maintain in the future.

This is the results when reading:

Please notice the pencil is misplaced between the label and the data.

And this is when updating:

This helps for now. I hope somebody comes with a better idea.

1 Like

Hello(hola!)

Have you tried with hooks?

I can give you a hand if you need it.

Hello! Thanks for your response. I am not familiar with hooks in Dolibarr. Can you please tell me how to use them?

First of all, you need to create a new module. Then create a file in your class folder. Usually named actions_myobjectname.class.php. Then you have to identify the hook you want to use. You can go to the core file. if is a customer invoice, customer order, supplier order, etc. You to go to card.php file and search for initHook(). There is the name of the hook. Like invoicedcard is for customer invoices.

then in your file /custom/mymodule/core/modMymodule.class.php find the like hooks in _construct method. Like this:

$this->module_parts = array(
‘hooks’ => array(
‘data’ => array(
‘invoicecard’,
),
),

then in yourt hooks file you can create something like this:

public function formObjectOptions($parameters, &$object, &$action, $hookmanager)
{
global $conf, $user, $langs;

if (in_array('invoicecard', explode(':', $parameters['context']))) {

    // Depende de la acción (ver, crear, editar, etc.)
    if ($action == 'create' || $action == 'edit' || $action == '') {

        // Puedes insertar aquí tu JavaScript
        print '
            <script type="text/javascript">
                $(document).ready(function() {
                    $("#options_extrafieldname").parent().parent().prependTo(
                        $("#idofthefieldwillbebelowofextrafield").parent().parent().parent()
                    );
                });
            </script>
        ';
    }
}

}

And that works for me.

Thanks for the example. But I can’t understand. Only part of the code in your response is highlighted as code. Is that the only part that I need to implement?

Do I have to implement all the code starting from “public function formObject…”?

I am sorry. I couldn’t understand what to implement.

Ok I am trying to create my Hooks module.

Each time I click the pencil icon beside the module description file named: ubicaprovcf/core/modules/modUbicaprovcf.class.php, I am sent to the specific line 121 in the code.

After that, I don’t know where to drop the code.

I prepared this Fiddle to review the code. I see some errors.

https://jsfiddle.net/geppettvs/tL7c1fh9/1/

Can you please help?