add room/building to addreses

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jamie
    Senior Member
    • Aug 2025
    • 342

    #1

    add room/building to addreses

    It would be useful to us to have an option to add a room/building to the address, as they are often needed.
    Yes, I can add to the street part, though this can make that field very big and will sometimes fail validation and mess up the data a bit.

    something like
    • street part one
    • street part two

    would also work
  • eymen-elkum
    Active Community Member
    • Nov 2014
    • 506

    #2
    This can be implemented in an update-safe way by extending the Address field type with separate `building` and `room` attributes, then using a custom Address view to display and save them.

    1. Extend the Address field definition

    Create:

    Code:
    custom/Espo/Modules/ModuleName/Resources/metadata/fields/address.json
    Add:

    Code:
    {
    "view": "module-name:views/fields/address",
    "actualFields": [
    "__APPEND__",
    "building",
    "room"
    ],
    "fields": {
    "building": {
    "type": "varchar",
    "maxLength": 100,
    "pattern": "$noBadCharacters"
    },
    "room": {
    "type": "varchar",
    "maxLength": 100,
    "pattern": "$noBadCharacters"
    }
    }
    }
    The append marker must be written exactly as `__APPEND__`, with two underscores on each side.

    The `params` section is not required for storing these values. It is used only when adding configurable Address-field parameters to the Entity Manager. The actual subfields are defined through `actualFields` and `fields`.

    For a field named `billingAddress`, EspoCRM will generate attributes such as:

    Code:
    billingAddressBuilding
    billingAddressRoom
    For `shippingAddress`, it will generate:

    Code:
    shippingAddressBuilding
    shippingAddressRoom
    2. Create a custom Address view

    The standard Address view recognizes additional entries in `actualFields` and makes `buildingValue` and `roomValue` available to the template. However, its standard `fetch()` method saves only Street, City, State, Postal Code and Country.

    Create:

    Code:
    client/custom/modules/module-name/src/views/fields/address.js
    Add:

    Code:
    import AddressFieldView from 'views/fields/address';
    
    export default class extends AddressFieldView {
    
    setup() {
    super.setup();
    
    this.editTemplate1 = 'module-name:fields/address/edit-1';
    this.editTemplate2 = 'module-name:fields/address/edit-2';
    this.editTemplate3 = 'module-name:fields/address/edit-3';
    this.editTemplate4 = 'module-name:fields/address/edit-4';
    }
    
    afterRender() {
    super.afterRender();
    
    if (this.mode !== this.MODE_EDIT) {
    return;
    }
    
    this.$building = this.$el.find(
    `[data-name="${this.buildingField}"]`
    );
    
    this.$room = this.$el.find(
    `[data-name="${this.roomField}"]`
    );
    
    this.$building.on('change', () => {
    this.trigger('change');
    });
    
    this.$room.on('change', () => {
    this.trigger('change');
    });
    }
    
    fetch() {
    const data = super.fetch();
    
    const building = this.$building.val();
    const room = this.$room.val();
    
    data[this.buildingField] = building
    ? building.toString().trim()
    : null;
    
    data[this.roomField] = room
    ? room.toString().trim()
    : null;
    
    return data;
    }
    }
    Because the new attributes are listed in `actualFields`, the parent Address view automatically creates properties such as:

    Code:
    this.buildingField
    this.roomField
    It also supplies these template values:

    Code:
    buildingValue
    roomValue
    3. Create the Address templates

    EspoCRM uses a numbered edit template according to Administration > Settings > Address Format.

    Copy the relevant standard template into:

    Code:
    client/custom/modules/module-name/res/templates/fields/address/
    The available formats are:

    Code:
    edit-1.tpl
    edit-2.tpl
    edit-3.tpl
    edit-4.tpl
    Copy the complete original template first, then insert the additional inputs in the required position:

    Code:
    <div class="row">
    <div class="col-sm-6 col-xs-6">
    <input
    type="text"
    class="form-control"
    data-name="{{name}}Building"
    value="{{buildingValue}}"
    placeholder="{{translate 'Building'}}"
    maxlength="100"
    >
    </div>
    
    <div class="col-sm-6 col-xs-6">
    <input
    type="text"
    class="form-control"
    data-name="{{name}}Room"
    value="{{roomValue}}"
    placeholder="{{translate 'Room'}}"
    maxlength="100"
    >
    </div>
    </div>
    You only need to customize the format used by your installation, but providing all four templates makes the extension work if the administrator changes the Address Format later.

    4. Add translations and rebuild

    Add the Building and Room labels to the module language files, then rebuild EspoCRM and transpile or bundle the frontend view:

    Code:
    php rebuild.php
    Test creating and editing a record, then confirm through the API that attributes such as `billingAddressBuilding` and `billingAddressRoom` are saved and returned.

    This covers storing and editing the new values. If Building and Room must also appear in detail/list views, PDFs, emails, maps or backend-generated formatted addresses, the client display logic and the server-side formatter for the selected Address Format should also be extended.

    This solution avoids putting everything into Street, keeps the values independently searchable and exportable, and does not require modifying EspoCRM core files.
    Last edited by eymen-elkum; Today, 06:48 AM.
    Eblasoft | EspoCRM specialists since 2014
    Consulting · Development · Integrations · Premium Extensions
    .

    Comment

    Working...