Documentation

Dialog class

Main class for adding items, tabs and footer to dialog.

Method Description Available from version
static getSelectedItems(array $ids, array $options = []): ItemCollection Static method, returns collection of items (object of class ItemCollection) by specified identifiers.
  • $ids — array with IDs, with each item presenting an array with two items ['entityId', 'elementId'].
  • $options — array with entity options. Each item us an array type:
    [
      'id' => 'entityId', // entity identifier
      'options' => [] // entity options
    ]
    

    This static method is similar to the method getItems, but is called for known preselected items (опция preselectedItems).

Difference between getItems and getSelectedItems can be most conveniently demonstrated by the example of "user" entity. For example, there is a blog post entry, addressed to several users. When editing this entity, users indicated in the "To" field must all be displayed, even if dismissed (inactive) users are included among them as well. However, there shouldn't be any dismissed users in the "Recent" list or at the tab with company structure. Method getSelectedItems returns data on dismissed users, and getItems won't.

This method is commonly used to pre-select item data at the backend and throw it to JavaScript.

$preselectedItems = [
  ['user', 1],
  ['department', 1],
  ['user', 7],
  ['project', 2],
  ['department', '2:F'],
  ['meta-user', 'all-users'],
  ['user', 27],
];

$options = [
  ['id' => 'user', 'options' => ['nameTemplate' => '#LAST_NAME#']]
];


$items = \Bitrix\UI\EntitySelector\Dialog::getItems($preselectedItems, $options);
$selectedItems = $items->toJsObject();

<script>
const tagSelector = new TagSelector({
    dialogOptions: {
        context: 'MY_MODULE_CONTEXT',
        selectedItems: ,
        entities: [
            {
                id: 'user', // users
            },
            {
                id: 'project', // groups and projects
            },
            {
                id: 'department', // company structure
                options: {
                    selectMode: 'usersAndDepartments' // user and department selection
                }
            },
            {
                id: 'meta-user',
                options: {
                    'all-users': true // All users
                }
            },
        ],
    }
});

tagSelector.renderTo(document.getElementById('container'));

static getItems(array $ids, array $options = []): ItemCollection Static method, returns collection of items (object of class ItemCollection) by specified identifiers.
  • $ids — array with identifiers, with each item as an array of two items ['entityId', 'elementId'].
  • $options — array with entity options. Each array item is an array itself as follows:

    [
      'id' => 'entityId', // entity identifier
      'options' => [] // entity options
    ]
    

    This method is commonly used to pre-select item data at the backend and throw it to JavaScript.

getId(): ?string Returns dialog ID.
getContext(): ?string Returns identifier for context.
getCurrentUserId(): int Returns identifier for current user.
addItem(Item $item) Adds an item to dialog.
  • $item — added item (object of class Item).

public function fillDialog(Dialog $dialog): void
{
    $dialog->addTab(new Tab([
        'id' => 'my-tab',
        'title' => 'My tab',
        'stub' => true,
        'icon' => [
            'default' => '/path/to/tab-icon.svg',
            'selected' => '/path/to/tab-icon-selected.svg'
        ]
    ]));
    
    $dialog->addItem(
        new Item([
            'id' => 1000,
            'entityId' => 'my-entity-id',
            'tabs' => 'my-tab',
            'title' => 'My item',
            'avatar' => '/path/to/avatar.jpg',
            'customData' => [
                'myOption' => true,
            ],
        ])
    );
}

addItems(array $items) Adds items to dialog.
  • $items — array with added items (objects of class Item).
addRecentItem(Item $item) Adds item to the "Recent" tab.
  • $item — added item (object of class Item).
addRecentItems(array $items) Adds items to the "Recent" tab.
  • $items — array with added items (objects of class Item).

public function fillDialog(Dialog $dialog): void
{
    $dialog->addRecentItems([
        new Item([
            'id' => 1000,
            'entityId' => 'my-entity-id',
            'entityType' => 'active',
            'title' => 'My item',
            'avatar' => '/path/to/avatar.jpg',
            'customData' => [
                'myOption' => true,
            ],
        ]),
        new Item([
            'id' => 2000,
            'entityId' => 'my-entity-id',
            'entityType' => 'active',
            'title' => 'My item 2',
            'avatar' => '/path/to/avatar.jpg',
            'customData' => [
                'myStringOption' => 'kuku',
            ],
        ])
    ]);
}

getRecentItems(): RecentCollection Returns collection of type ItemCollection with item list at the "Recent" tab that were selected in the current context. Data for collection are selected automatically by the dialog.

public function fillDialog(Dialog $dialog): void
{
    // select all items for my item at the "Recent" tab
    $recentItems = $dialog->getRecentItems()->getEntityItems('my-entity-id');
    if ($recentItems < 10)
    {
        // when items are few, add more.
        $dialog->addRecentItem(new Item([
            'id' => 1000,
            'entityId' => 'my-entity-id',
            'entityType' => 'active',
            'title' => 'My item',
            'avatar' => '/path/to/avatar.jpg',
            'customData' => [
                'myOption' => true,
            ],
        ]));
    }
}

getGlobalRecentItems(): RecentCollection Returns collection of type ItemCollection with item list at the "Recent" tab, selected in the global context. Data for collection is selected by the dialog automatically.

public function fillDialog(Dialog $dialog): void
{
    $maxItemsInRecentTab = 30;

    // select all items for my item at the "Recent" tab for current context.
    $recentItems = $dialog->getRecentItems()->getEntityItems('my-entity-id');
    if (count($recentItems) < $maxItemsInRecentTab)
    {
        $limit = $maxItemsInRecentTab - count($recentItems);

        // select all items for my item at the "Recent" tab for global context.
        $recentGlobalItems = $dialog->getGlobalRecentItems()->getEntityItems('my-entity-id');
        foreach ($recentGlobalItems as $recentGlobalItem)
        {
            if ($limit <= 0)
            {
                break;
            }

            if (!isset($recentItems[$recentGlobalItem->getId()]) && $recentGlobalItem->isLoaded())
            {
                // add items from global context at the "Recent" tab.
                $dialog->getRecentItems()->add($recentGlobalItem);
                $limit--;
            }
        }
    }
}

getItemCollection(): ItemCollection Returns collection of type ItemCollection with list of all added items (objects of class Item) в диалог.
setFooter(string $footer, array $options = []) Sets dialog footer.
  • $footer — dialog footer. Indicates either layout or full name for JavaScript class, which implements footer rendering.
  • $options — additional footer options.

public function fillDialog(Dialog $dialog): void
{
    $dialog->setFooter('BX.SocialNetwork.EntitySelector.Footer', ['myOption' => true]);
}

getFooter(): ?string Returns dialog footer.
getFooterOptions(): ?array Returns additional footer options.
addTab(Tab $tab) Adds a new tab to dialog.
  • $tab — added tab (object of class Tab).

public function fillDialog(Dialog $dialog): void
{
    $dialog->addTab(new Tab([
        'id' => 'my-tab',
        'title' => 'My tab',
        'stub' => true,
        'icon' => [
            'default' => '/path/to/tab-icon.svg',
            'selected' => '/path/to/tab-icon-selected.svg'
        ]
    ]));
}

getTabs(): array Returns array with dialog tabs (objects of class Tab).
getTab(string $tabId): ?Tab Returns tab object by ID.
  • $tabId — tab ID.
addEntity(Entity $entity) Adds new entity to dialog.
  • $entity — new entity (objects of class Entity).
getEntities(): array Returns array with dialog entities (objects of class Entity).
getEntity(string $entityId): ?Entity Returns entity object by ID.
  • $entityId — entity ID.

public function fillDialog(Dialog $dialog): void
{
    // when entity is single at the dialog, add items to "Recent" tab.
    if (count($dialog->getEntities()) === 1)
    {
        $dialog->addRecentItems([
            new Item([
                'id' => 1000,
                'entityId' => 'my-entity-id',
                'entityType' => 'active',
                'title' => 'My item',
                'avatar' => '/path/to/avatar.jpg',
                'customData' => [
                    'myOption' => true,
                ],
            ]),
            new Item([
                'id' => 2000,
                'entityId' => 'my-entity-id',
                'entityType' => 'active',
                'title' => 'My item 2',
                'avatar' => '/path/to/avatar.jpg',
                'customData' => [
                    'myStringOption' => 'kuku',
                ],
            ])
        ]);
    }
}


© «Bitrix24», 2001-2024
Up