### ElementPage get() Method Example (Twig)
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementPage
Demonstrates how to retrieve an ElementItem object using the ElementPage component's get() method in a Twig template. The method fetches a model object based on a 'slug' parameter.
```twig
[ElementPage]
slug = "{{ :slug }}"
==
{% set obItem = ElementPage.get() %}
{{ obItem.name }}
{{ obItem.description|raw }}
```
--------------------------------
### ElementItem Class Extension Example
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementItem
Demonstrates how to extend the ElementItem class with custom fields and dynamic methods, leveraging the constructor extension mechanism.
```php
ElementModel::extend(function($obModel) {
// Add custom fields to the cached array
$obModel->addCachedField(['field_1', 'field_2']);
});
ElementItem::extend(function($obItem) {
// Add a custom property to the item
$obItem->arExtendResult[] = 'addMyProperty';
// Define a dynamic method to set the custom property
$obItem->addDynamicMethod('addMyProperty', function() use ($obItem) {
$obModel = $obItem->getObject();
$obItem->setAttribute('my_property', $obModel->my_property);
});
});
```
--------------------------------
### Get First Element
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method returns the first ElementItem object found in the collection.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->first();
```
--------------------------------
### Get All Element Items
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method returns an array containing all ElementItem objects currently in the collection.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
$arElementIDList = [2,14,18];
$obList->merge($arElementIDList);
```
--------------------------------
### Take Subset of Elements
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method returns a specified count of ElementItem objects, starting from the position defined by the skip() method. A count of 0 returns all remaining elements.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->skip(2)->take(1);
```
--------------------------------
### Get Elements Per Page - Pagination
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/Components
Retrieves the number of elements to be displayed on a single page, based on component settings. This value can be overridden by providing a 'limit' parameter in the request.
```APIDOC
getCountPerPage(): int
Description: Retrieves the count of elements per page from component settings.
Overrides: Accepts a 'limit' request parameter to override the default setting.
Returns: The number of elements per page.
```
--------------------------------
### Twig Partial for AJAX Update
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementData
A Twig template snippet designed to be used as a partial in an AJAX response. It retrieves an ElementItem based on an input ID and displays its details, similar to the initial get() example.
```twig
{# my_partial.htm #}
{# Retrieve an ElementItem using an input ID passed via AJAX #}
{% set obItem = ElementData.get(input('element_id')) %}
{# Display the item's details if it exists #}
{% if obItem.isNotEmpty() %}
{{ obItem.name }}
{{ obItem.description|raw }}
{% endif%}
```
--------------------------------
### Get ElementItem by ID (Twig)
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementData
Demonstrates how to retrieve an ElementItem object using the ElementData.get() method and display its properties. Requires the ElementData class to be accessible.
```twig
{# Get an ElementItem object by its ID #}
{% set obItem = ElementData.get(1) %}
{# Check if the item is not empty before displaying its content #}
{% if obItem.isNotEmpty() %}
{{ obItem.name }}
{{ obItem.description|raw }}
{% endif%}
```
--------------------------------
### Get List of Element IDs
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method returns an array containing all the element IDs currently stored in the collection.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->getIDList();
```
--------------------------------
### Get Current Page from Request - Pagination
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/Components
Retrieves the current page number from the request parameters. It expects the page number to be sent with the key 'page'.
```APIDOC
getPageFromRequest(): int
Description: Gets the current page number from the request parameter 'page'.
Returns: The current page number as an integer.
```
--------------------------------
### Get Random Elements
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method returns an array of randomly selected ElementItem objects from the collection.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->random(2);
```
--------------------------------
### Get Pagination Buttons Array - Pagination
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/Components
Generates and returns an array containing the data for pagination buttons, suitable for rendering navigation links. It requires the current page and total element count.
```APIDOC
get(iPage: int, iCount: int): array
Description: Generates an array of pagination buttons for navigation.
Parameters:
- iPage: The current page number.
- iCount: The total available count of elements.
Returns: An array where each element contains button details (e.g., 'value', 'class', 'name').
Usage Example:
{% set iPage = Pagination.getPageFromRequest() %}
{% set iCount = ElementList.count() %}
{% set arPaginationList = Pagination.get(iPage, iCount) %}
{% if arPaginationList is not empty %}
{% for arPagination in arPaginationList %}
{% endfor %}
{% endif %}
```
--------------------------------
### Get Count for Next Page - Pagination
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/Components
Determines the number of elements that will be displayed on the next page. This method is useful for implementing 'Show More' functionality.
```APIDOC
getCountForNextPage(iPage: int, iCount: int): int
Description: Calculates the count of elements available for the next page.
Parameters:
- iPage: The current page number.
- iCount: The total available count of elements.
Returns: The count of elements for the next page.
Usage Example:
{% set iPage = Pagination.getPageFromRequest() %}
{% set iCount = ElementCollection.make().active().count() %}
{% set iNextPageCount = Pagination.getCountForNextPage(iPage, iCount) %}
{% if iNextPageCount > 0 %}
Show more
{% endif %}
```
--------------------------------
### Get Maximum Page Number - Pagination
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/Components
Calculates the total number of pages available based on the total count of elements. This is often used to determine if a 'Show More' button should be displayed.
```APIDOC
getMaxPage(iCount: int): int
Description: Calculates the maximum page number based on the total available elements.
Parameters:
- iCount: The total available count of elements.
Returns: The number of the last page.
Usage Example:
{% set iPage = Pagination.getPageFromRequest() %}
{% set iCount = ElementCollection.make().active().count() %}
{% set iMaxPage = Pagination.getMaxPage(iCount) %}
{% if iMaxPage > iPage %}
Show more
{% endif %}
```
--------------------------------
### Create ElementCollection Instance
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Static method to create a new instance of the ElementCollection class, optionally initializing it with a list of element IDs.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
```
--------------------------------
### ElementItem Class API Documentation
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementItem
Provides a comprehensive overview of the ElementItem class methods for data caching, creation, and manipulation.
```APIDOC
ElementItem Class Methods:
make($iElementID, $obElement = null)
- Creates a new ElementItem object using cached data.
- Parameters:
- iElementID: int - The model element ID.
- obElement: object | null - The model object (optional).
- Returns: ElementItem - An instance of the ElementItem class.
- Example:
$obItem = ElementItem::make(1);
echo $obItem->id;
makeNoCache($iElementID, $obElement = null)
- Creates a new ElementItem object without using the cache.
- Parameters:
- iElementID: int - The model element ID.
- obElement: object | null - The model object (optional).
- Returns: ElementItem - An instance of the ElementItem class.
- Example:
$obItem = ElementItem::makeNoCache(1);
echo $obItem->id;
clearCache($iElementID)
- Clears the cache for a specific element.
- Parameters:
- iElementID: int - The model element ID.
- Example:
ElementItem::clearCache(1);
isEmpty()
- Checks if the element's data could not be obtained from object or cache.
- Returns: bool - True if data retrieval failed, false otherwise.
- Example:
$obItem = ElementItem::make(10);
if($obItem->isEmpty()) {
return false;
}
isNotEmpty()
- Checks if the element's data was successfully obtained from object or cache.
- Returns: bool - True if data retrieval was successful, false otherwise.
- Example:
$obItem = ElementItem::make(10);
if($obItem->isNotEmpty()) {
//...
}
toArray()
- Returns the element's data as an array.
- Returns: array - The element's data array.
- Example:
$obItem = ElementItem::make(10);
return $obItem->toArray();
toJSON()
- Returns the element's data array as a JSON string.
- Returns: string - The JSON representation of the element's data.
- Example:
$obItem = ElementItem::make(10);
return $obItem->toJSON();
getObject()
- Returns the underlying model's object.
- Returns: object - The model object.
- Example:
$obItem = ElementItem::make(10);
$obModel = $obItem->getObject();
getLangAttribute(string $attributeName, string $locale)
- Retrieves the value of a translatable attribute for a specific locale.
- Parameters:
- attributeName: string - The name of the attribute to retrieve.
- locale: string - The locale code (e.g., 'en', 'ru').
- Returns: mixed - The attribute value for the specified locale.
- Example:
echo $obElementItem->getLangAttribute( 'name', 'ru');
```
--------------------------------
### Handle AJAX Data Retrieval (JavaScript)
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementData
Shows how to make an AJAX request to the ElementData::onGetData() method to fetch item data. The response is logged to the console. Assumes jQuery and the $.request helper are available.
```javascript
// Make an AJAX request to fetch data using ElementData::onGetData
$.request('ElementData::onGetData', {
data: {'element_id': 1},
success: function(responce) {
// Log the received response data to the console
console.log(responce);
}
});
```
--------------------------------
### Skip Elements and Take Subset
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method is used in conjunction with take() to skip a specified number of elements before selecting a subset.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->skip(2)->take(1);
```
--------------------------------
### PHP ElementCollection extend() Method
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Enables adding custom methods and properties to the ElementCollection class using constructor extension. This allows for dynamic method addition to the collection instance.
```php
ElementCollection::extend(function($obCollection) {
$obCollection->addDynamicMethod('my_method', function($arElementIDList) use ($obCollection) {
return $obCollection->diff($arElementIDList);
});
});
```
--------------------------------
### PHP ElementCollection shift() Method
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Returns the first ElementItem object from a collection and removes it from the collection. Requires an initialized ElementCollection.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->shift();
```
--------------------------------
### PHP ElementCollection unshift() Method
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Adds an element to the beginning of the collection. This method modifies the collection in place and requires an ElementCollection instance and the element ID to add.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
$obList->unshift(4);
```
--------------------------------
### PHP ElementCollection push() Method
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Adds an element to the end of the collection. This method modifies the collection in place and requires an ElementCollection instance and the element ID to add.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
$obList->push(4);
```
--------------------------------
### Paginate Collection
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method returns an array of ElementItem objects for a specific page number, with a configurable number of elements per page.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->page(2, 10);
```
--------------------------------
### Handle Empty AJAX Request (JavaScript)
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementData
Illustrates making an AJAX request to an empty ElementData::onAjaxRequest() method, often used as a placeholder or for triggering specific client-side logic. It includes a partial update configuration.
```javascript
// Make an AJAX request to an empty method for potential future logic or triggers
$.request('ElementData::onAjaxRequest', {
data: {'element_id': 1},
// Specify a partial to update and the target selector
update: {'my_partial': '.my_class'}
});
```
--------------------------------
### Pagination Component Overview
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/Components
The Pagination component is designed to generate pagination buttons and offers flexible configuration. It relies on the 'oc-pagination' package for its functionality.
```APIDOC
Pagination Component:
Description: Provides pagination functionality and flexible settings.
Dependencies: Uses the 'oc-pagination' package.
Purpose: To manage and display page navigation elements.
```
--------------------------------
### PHP ElementCollection save() and saved() Methods
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
The save() method stores the current state of a collection using a specified key. The saved() method retrieves a previously saved collection state. These methods facilitate state persistence and retrieval.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
$obList->save('my_key');
...
$obSavedList = ElementCollection::make()->saved('my_key');
//result: $obSavedList == clone $obList
```
--------------------------------
### Accessing Translatable Fields
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementItem
Shows how to access translatable fields directly or retrieve them for a specific language using the getLangAttribute method.
```php
// Accessing the 'name' field for the active language
echo $obElementItem->name;
// Accessing the 'name' field for a specific language (e.g., Russian)
echo $obElementItem->getLangAttribute( 'name', 'ru');
```
--------------------------------
### PHP ElementCollection getNearestNext() Method
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Returns a new collection containing elements nearest to a given element ID, in the forward direction. Supports specifying the count of elements and cyclic behavior. Requires an ElementCollection, an element ID, and optional count and cyclic flags.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
//Collection has elements: 10
$obNewList = $obList->getNearestNext(2);
//Collection has elements: 2,10,15
$obNewList = $obList->getNearestNext(1, 3);
//Collection has elements: 10,15
$obNewList = $obList->getNearestNext(2, 3);
//Collection has elements: 10,15,1
$obNewList = $obList->getNearestNext(2, 3, true);
```
--------------------------------
### PHP ElementCollection implode() Method
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Returns a string by joining the values of a specified field name from each item in the collection, using a given delimiter. Requires an ElementCollection, field name, and an optional delimiter.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->implode('name', '-');
```
--------------------------------
### PHP ElementCollection debug() Method
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Allows setting a breakpoint for xDebug when collection methods are called within Twig templates. This method is used for debugging purposes and can be chained with other collection methods.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
$obList->skip(2)->debug()->take(2);
```
--------------------------------
### PHP ElementCollection getNearestPrev() Method
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Returns a new collection containing elements nearest to a given element ID, in the backward direction. Supports specifying the count of elements and cyclic behavior. Requires an ElementCollection, an element ID, and optional count and cyclic flags.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
//Collection has elements: 1
$obNewList = $obList->getNearestPrev(2);
//Collection has elements: 10,2,1
$obNewList = $obList->getNearestPrev(15, 3);
//Collection has elements: 2,1
$obNewList = $obList->getNearestPrev(10, 3);
//Collection has elements: 2,1,15
$obNewList = $obList->getNearestPrev(10, 3, true);
```
--------------------------------
### PHP ElementCollection pluck() Method
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Returns an array containing the values for a specified field name from each item in the collection. Requires an ElementCollection and the field name as a string.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->pluck('name');
```
--------------------------------
### PHP ElementCollection pop() Method
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Returns the last ElementItem object from a collection and removes it from the collection. Note: The provided description states 'first ElementItem', but standard 'pop' behavior and context suggest the last element. Requires an initialized ElementCollection.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->pop();
```
--------------------------------
### Count Elements in Collection
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method returns the total number of elements currently present in the collection.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->count();
```
--------------------------------
### Difference with Array
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method applies the array_diff() function to find elements present in the collection but not in the provided array of IDs.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
$arElementIDList = [2,14,18];
$obList->diff($arElementIDList);
```
--------------------------------
### Check if Collection is Empty
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method returns true if the element list within the collection is empty, false otherwise.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
if($obList->isEmpty()) {
return false;
}
```
--------------------------------
### Merge Collection with Array
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method applies the array_merge() function to combine the collection's IDs with a provided array of IDs.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
$arElementIDList = [2,14,18];
$obList->merge($arElementIDList);
```
--------------------------------
### Find Element Item by ID
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method returns the ElementItem object corresponding to the specified element ID, if found.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->find(10);
```
--------------------------------
### Apply Sorting with Array
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method applies an intersection operation between a provided array of element IDs and the collection's current IDs, potentially for sorting or filtering.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
$arElementIDList = [2,14,18];
$obList->applySorting($arElementIDList);
```
--------------------------------
### Check if Collection is Not Empty
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method returns true if the element list within the collection is not empty, false otherwise.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
if($obList->isNotEmpty()) {
//...
}
```
--------------------------------
### Clear Collection
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method removes all elements from the collection, making it empty.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
$obList->clear();
```
--------------------------------
### PHP ElementCollection last() Method
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Returns the last ElementItem object in a collection. This method operates on an existing ElementCollection instance.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
return $obList->last();
```
--------------------------------
### Set Element IDs in Collection
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method to set or replace the list of element IDs within an existing ElementCollection object.
```php
$obList = ElementCollection::make()->set([1,2]);
```
--------------------------------
### Intersect Collection with Array
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method applies the array_intersect() function to the collection's IDs and a provided array of IDs, keeping only common elements.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
$arElementIDList = [2,14,18];
$obList->intersect($arElementIDList);
```
--------------------------------
### Exclude Element by ID
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method removes a specific element from the collection based on its ID.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
$obList->exclude(2);
```
--------------------------------
### Check if Collection Has Element by ID
Source: https://github.com/oc-shopaholic/oc-toolbox-plugin/wiki/ElementCollection
Method returns true if the collection contains an element with the specified ID, false otherwise.
```php
$obList = ElementCollection::make([1, 2, 10, 15]);
if($obList->has(10)) {
//...
}
```
=== COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.