### Install Block Builder with Composer
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/README.md
Instructions for installing the Block Builder addon using Composer. This command downloads and installs the latest version of the addon.
```bash
composer require parasek/block_builder
```
--------------------------------
### Display Option List Attribute
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/express_basic.txt
Iterates through and displays items from an 'example_option_list' attribute associated with an entry. Checks if the attribute is not empty.
```php
getAttribute('example_option_list'))): ?>
getAttribute('example_option_list') as $item): ?>
```
--------------------------------
### Displaying Concrete CMS Express Entry Attributes
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/express_basic.txt
This snippet shows how to fetch an Express entry and display various attributes like text, duration, rich text, checkbox, and date/time. It includes examples of basic display, custom formatting for dates, and retrieving checkbox labels.
```php
Text:
getAttribute('example_text'))): ?>
getAttribute('example_text')); ?>
Duration:
getAttribute('example_duration'))): ?>
getAttribute('example_duration')); ?>
Text Area:
getAttribute('example_text_area'))): ?>
Plain Text:
getAttribute('example_text_area'))); ?>
------------------------------------------------------------
Rich Text - Default Setting:
getAttribute('example_text_area'); ?>
Checkbox:
getAttribute('example_checkbox'))): ?>
getAttribute('example_checkbox')); ?>
------------------------------------------------------------
Label:
getAttributeValueObject('example_checkbox')->getAttributeKey();
$type = $key->getAttributeKeySettings();
/** @var \Concrete\Core\Entity\Attribute\Key\Settings\BooleanSettings $type */
$label = $type->getCheckboxLabel();
?>
Date/Time:
getAttribute('example_date_time'))): ?>
Default Date (honours multilingual settings or use "Custom format"):
getAttribute('example_date_time', 'display')); ?>
------------------------------------------------------------
Custom format:
getAttributeValueObject('example_date_time')->getPlainTextValue()))); ?>
Image/File:
getAttribute('example_image_file'))): ?>
getAttribute('example_image_file'));
/** @var \Concrete\Core\Entity\File\Version $file */
?>
Basic info:
getFileID() . '
';
echo 'File Name: ' . $file->getFileName() . '
';
echo 'Full URL: ' . $file->getURL() . '
';
echo 'Tracked URL: ' . $file->getDownloadURL() . '
';
echo 'Relative Path: ' . $file->getRelativePath() . '
';
echo 'Full Relative Path: ' . $_SERVER['DOCUMENT_ROOT'] . $file->getRelativePath() . '
';
echo 'Title: ' . $file->getTitle() . '
';
echo 'Description: ' . $file->getDescription() . '
';
echo 'Tags (string): ' . $file->getTags() . '
';
echo 'Tags (array): ' . '
';
var_dump($file->getTagsList());
echo '
';
echo 'File Size: ' . $file->getSize() . '
';
echo 'Full File Size: ' . $file->getFullSize() . '
';
echo 'Extension: ' . $file->getExtension() . '
';
echo 'Type: ' . $file->getType() . '
';
echo 'Mime: ' . $file->getMimeType() . '
';
echo 'Display Type: ' . $file->getDisplayType() . '
';
echo 'Generic Type Text: ' . $file->getGenericTypeText() . '
';
echo 'Width: ' . $file->getAttribute('width') . '
';
echo 'Height: ' . $file->getAttribute('height') . '
';
echo 'Duration: ' . $file->getAttribute('duration') . '
';
?>
------------------------------------------------------------
```
--------------------------------
### Install Block Builder for Concrete5.8
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/README.md
Specific Composer command to install version 1.4.0 of the Block Builder addon, intended for use with Concrete5.8.
```bash
composer require parasek/block_builder:^1.4.0
```
--------------------------------
### Display Page Selector Attribute
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/express_basic.txt
Retrieves details of a page selected via the 'example_page_selector' attribute and displays its ID, name, description, link, handle, and a custom page attribute. It includes checks for valid page objects and ensures the page is not in the trash.
```php
getAttribute('example_page_selector'))):
?>
Default Date (honours multilingual settings or use "Custom format"):
getAttribute('example_page_selector')) ?>
isError() and !$page->isInTrash()): ?>
getCollectionID() . '
';
echo 'Name: ' . $page->getCollectionName() . '
';
echo 'Description: ' . $page->getCollectionDescription() . '
';
echo 'Link: ' . $page->getCollectionLink() . '
';
echo 'Handle: ' . $page->getCollectionHandle() . '
';
echo 'Example Page Attribute: ' . $page->getAttribute('page_attribute_handle') . '
';
?>
```
--------------------------------
### Generate Thumbnail from Image Helper
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/express_basic.txt
Generates a thumbnail for a given file using the Image Helper and displays it. Requires the file object to be valid and editable.
```php
canEdit()): ?>
make('helper/image')->getThumbnail($file, 100, 75, true);
?>
```
--------------------------------
### Display Option List Attribute
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/express_repeatable.txt
Iterates through and displays items from an 'example_option_list' attribute associated with an entry. This is useful for displaying lists of text values. It checks if the attribute is not empty before processing.
```php
getAttribute('example_option_list') as $item):
echo h($item); ?>
```
--------------------------------
### Displaying Express Entry Attributes in Concrete CMS
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/express_repeatable.txt
This PHP code snippet shows how to retrieve an Express entry by its handle and display various attributes, including text, duration, text area, checkbox, date/time, and image/file. It includes examples for formatting and accessing attribute values.
```php
Text:
getAttribute('example_text'))): ?>
getAttribute('example_text')); ?>
Duration:
getAttribute('example_duration'))): ?>
getAttribute('example_duration')); ?>
Text Area:
getAttribute('example_text_area'))): ?>
Plain Text:
getAttribute('example_text_area'))); ?>
------------------------------------------------------------
Rich Text - Default Setting:
getAttribute('example_text_area'); ?>
Checkbox:
getAttribute('example_checkbox'))): ?>
getAttribute('example_checkbox')); ?>
------------------------------------------------------------
Label:
getAttributeValueObject('example_checkbox')->getAttributeKey();
$type = $key->getAttributeKeySettings();
/** @var \Concrete\Core\Entity\Attribute\Key\Settings\BooleanSettings $type */
$label = $type->getCheckboxLabel();
?>
Date/Time:
getAttribute('example_date_time'))): ?>
Default Date (honours multilingual settings or use "Custom format"):
getAttribute('example_date_time', 'display')); ?>
------------------------------------------------------------
Custom format:
getAttributeValueObject('example_date_time')->getPlainTextValue()))); ?>
Image/File:
getAttribute('example_image_file'))): ?>
getAttribute('example_image_file'));
/** @var \Concrete\Core\Entity\File\Version $file */
?>
Basic info:
getFileID() . '
';
echo 'File Name: ' . $file->getFileName() . '
';
echo 'Full URL: ' . $file->getURL() . '
';
echo 'Tracked URL: ' . $file->getDownloadURL() . '
';
echo 'Relative Path: ' . $file->getRelativePath() . '
';
echo 'Full Relative Path: ' . $_SERVER['DOCUMENT_ROOT'] . $file->getRelativePath() . '
';
echo 'Title: ' . $file->getTitle() . '
';
echo 'Description: ' . $file->getDescription() . '
';
echo 'Tags (string): ' . $file->getTags() . '
';
echo 'Tags (array): ' . '
';
var_dump($file->getTagsList());
echo '
';
echo 'File Size: ' . $file->getSize() . '
';
echo 'Full File Size: ' . $file->getFullSize() . '
';
echo 'Extension: ' . $file->getExtension() . '
';
echo 'Type: ' . $file->getType() . '
';
endif;
?>
```
--------------------------------
### Display Concrete Thumbnail
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/express_basic.txt
Retrieves and displays a Concrete CMS thumbnail for a file using a predefined thumbnail type ('file_manager_listing'). Requires the file object to be valid and editable, and the thumbnail type to exist.
```php
canEdit()): ?>
getThumbnailURL($type->getBaseVersion()); // or $type->getDoubledVersion()
?>
```
--------------------------------
### Retrieve and Display Page Selector Information
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/express_repeatable.txt
Retrieves a page by its ID from an 'example_page_selector' attribute and displays its key properties, including ID, name, description, link, handle, and a specific page attribute ('page_attribute_handle'). It includes checks to ensure the page object is valid and not in the trash.
```php
getAttribute('example_page_selector')) ?>
isError() && !$page->isInTrash()): ?>
getCollectionID() . '
';
echo 'Name: ' . $page->getCollectionName() . '
';
echo 'Description: ' . $page->getCollectionDescription() . '
';
echo 'Link: ' . $page->getCollectionLink() . '
';
echo 'Handle: ' . $page->getCollectionHandle() . '
';
echo 'Example Page Attribute: ' . $page->getAttribute('page_attribute_handle') . '
';
?>
```
--------------------------------
### Generate Image Thumbnail using Helper
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/express_repeatable.txt
Generates and displays an image thumbnail for a given file using the Concrete CMS image helper. It specifies the desired thumbnail dimensions (100x75) and whether to preserve aspect ratio. Requires the file object to be editable.
```php
make('helper/image')->getThumbnail($file, 100, 75, true);
?>
```
--------------------------------
### Display Default Picture Tag
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/express_basic.txt
Renders the default picture tag for a file using the 'html/image' service. Requires the file object to be valid and editable.
```php
canEdit()): ?>
make('html/image', ['f' => $file])->getTag(); ?>
```
--------------------------------
### Display File Metadata and Attributes
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/express_repeatable.txt
Retrieves and displays various metadata and attributes of a file object, including MIME type, display type, generic type text, width, height, and duration. Assumes the existence of a file object and necessary Concrete CMS helpers.
```php
echo 'Mime: ' . $file->getMimeType() . '
';
echo 'Display Type: ' . $file->getDisplayType() . '
';
echo 'Generic Type Text: ' . $file->getGenericTypeText() . '
';
echo 'Width: ' . $file->getAttribute('width') . '
';
echo 'Height: ' . $file->getAttribute('height') . '
';
echo 'Duration: ' . $file->getAttribute('duration') . '
';
```
--------------------------------
### Render Default Picture Tag
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/express_repeatable.txt
Renders the default picture tag for a file using Concrete CMS's HTML image helper. This is useful for displaying images in a responsive manner. Requires the file object to be editable.
```php
make('html/image', ['f' => $file])->getTag(); ?>
```
--------------------------------
### Display Concrete CMS File Manager Thumbnail
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/express_repeatable.txt
Retrieves and displays a thumbnail for a file using a predefined Concrete CMS thumbnail type ('file_manager_listing'). This method is typically used for displaying files in the dashboard or system settings. Requires the file object to be editable and the thumbnail type to exist.
```php
getThumbnailURL($type->getBaseVersion()); // or $type->getDoubledVersion()
?>
```
--------------------------------
### Get Files by FileSet ID (PHP)
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/functions/get_files_by_file_set_id.txt
Retrieves a list of files associated with a given FileSet ID in Concrete CMS. It handles cases where the FileSet ID might be empty or invalid, and uses Concrete CMS's FileList to filter and sort the files.
```php
protected function getFilesByFileSetID($fileSetID)
{
$files = [];
if (!empty($fileSetID)) {
$fileSet = FileSet::getByID($fileSetID);
if (is_object($fileSet)) {
$list = new FileList();
$list->filterBySet($fileSet);
$list->sortByFileSetDisplayOrder();
$files = $list->getResults();
}
}
return $files;
}
```
--------------------------------
### Display File Attributes in Concrete CMS
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/file_set_repeatable.txt
This snippet shows how to iterate through a list of file entities and display their various properties such as ID, name, URL, size, type, and custom attributes. It also includes examples of how to retrieve and display tags, both as a string and an array.
```php
File Set ID:
------------------------------------------------------------
Basic info:
getFileID() . '
';
echo 'File Name: ' . $entry_[[[HANDLE]]]_file->getFileName() . '
';
echo 'Full URL: ' . $entry_[[[HANDLE]]]_file->getURL() . '
';
echo 'Tracked URL: ' . $entry_[[[HANDLE]]]_file->getDownloadURL() . '
';
echo 'Relative Path: ' . $entry_[[[HANDLE]]]_file->getRelativePath() . '
';
echo 'Full Relative Path: ' . $_SERVER['DOCUMENT_ROOT'] . $entry_[[[HANDLE]]]_file->getRelativePath() . '
';
echo 'Title: ' . $entry_[[[HANDLE]]]_file->getTitle() . '
';
echo 'Description: ' . $entry_[[[HANDLE]]]_file->getDescription() . '
';
echo 'Tags (string): ' . $entry_[[[HANDLE]]]_file->getTags() . '
';
echo 'Tags (array): ' . '
';
var_dump($entry_[[[HANDLE]]]_file->getTagsList());
echo '
';
echo 'File Size: ' . $entry_[[[HANDLE]]]_file->getSize() . '
';
echo 'Full File Size: ' . $entry_[[[HANDLE]]]_file->getFullSize() . '
';
echo 'Extension: ' . $entry_[[[HANDLE]]]_file->getExtension() . '
';
echo 'Type: ' . $entry_[[[HANDLE]]]_file->getType() . '
';
echo 'Mime: ' . $entry_[[[HANDLE]]]_file->getMimeType() . '
';
echo 'Display Type: ' . $entry_[[[HANDLE]]]_file->getDisplayType() . '
';
echo 'Generic Type Text: ' . $entry_[[[HANDLE]]]_file->getGenericTypeText() . '
';
echo 'Width: ' . $entry_[[[HANDLE]]]_file->getAttribute('width') . '
';
echo 'Height: ' . $entry_[[[HANDLE]]]_file->getAttribute('height') . '
';
echo 'Duration: ' . $entry_[[[HANDLE]]]_file->getAttribute('duration') . '
';
?>
------------------------------------------------------------
Thumbnail from Image Helper:
canEdit()): ?>
make('helper/image')->getThumbnail( $entry_[[[HANDLE]]]_file, 100, 75, true);
?>
------------------------------------------------------------
Default picture tag:
canEdit()): ?>
make('html/image', ['f' => $entry_[[[HANDLE]]]_file])->getTag(); ?>
------------------------------------------------------------
Concrete Thumbnail (Dashboard / System & Settings / Files / Thumbnails):
canEdit()): ?>
getThumbnailURL($type->getBaseVersion()); // or $type->getDoubledVersion()
?>
```
--------------------------------
### Prepare Image for View (PHP)
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/functions/prepare_for_view_image.txt
Prepares image data for display in a Concrete CMS block. It merges default options with user-provided options for fullscreen and thumbnail views, retrieves file information, and generates appropriate links and dimensions. Handles image resizing and cropping based on specified options.
```php
private function prepareForViewImage($type, $fields, $options = []) {
// Options
if (!is_array($options)) {
$options = [];
}
$defaultOptions = [];
$defaultOptions['fullscreen'] = false;
$defaultOptions['fullscreenWidth'] = 1920;
$defaultOptions['fullscreenHeight'] = 1080;
$defaultOptions['fullscreenCrop'] = false;
$defaultOptions['thumbnail'] = false;
$defaultOptions['thumbnailWidth'] = 480;
$defaultOptions['thumbnailHeight'] = 270;
$defaultOptions['thumbnailCrop'] = true;
$options = array_merge($defaultOptions, $options);
// Prepare links/images
$keys = array_keys($fields);
$fileIDFieldName = $keys[0];
$altFieldName = $keys[1];
$fileID = $fields[$fileIDFieldName];
$alt = $fields[$altFieldName];
$fileObject = false;
$filename = '';
$relativePath = '';
$fileType = '';
$link = '';
$width = '';
$height = '';
$fullscreenLink = '';
$fullscreenWidth = $options['fullscreenWidth'];
$fullscreenHeight = $options['thumbnailHeight'];
$thumbnailLink = '';
$thumbnailWidth = $options['thumbnailWidth'];
$thumbnailHeight = $options['thumbnailHeight'];
if (!empty($fileID)) {
$fileObject = File::getByID($fileID);
if (is_object($fileObject)) {
$filename = $fileObject->getFileName();
$fileType = $fileObject->getType();
$relativePath = $fileObject->getRelativePath();
if (empty($alt)) {
$alt = $fileObject->getTitle();
$removableExtensions = ['jpg', 'jpeg', 'png', 'tiff', 'svg', 'webp'];
$extension = strtolower(pathinfo($alt, PATHINFO_EXTENSION));
if (!empty($extension) and in_array($extension, $removableExtensions)) {
$alt = pathinfo($alt, PATHINFO_FILENAME); // Remove extension
$alt = preg_replace('/ - [0-9]*$/', '', $alt); // Remove counter at the end of file name, " - 001", " - 002" and so on.
}
}
// Original image
$link = $fileObject->getURL();
$width = $fileObject->canEdit() ? $fileObject->getAttribute('width') : $options['thumbnailWidth'];
$height = $fileObject->canEdit() ? $fileObject->getAttribute('height') : $options['thumbnailHeight'];
if ($fileObject->canEdit()) {
// Fullscreen image
if (!empty($options['fullscreen'])) {
$fullscreenWidth = $options['fullscreenWidth'];
$fullscreenHeight = $options['fullscreenHeight'];
$fullscreenCrop = $options['fullscreenCrop'];
if ($fileObject->canEdit() AND (($width > $fullscreenWidth AND $fullscreenWidth!=false) OR ($height > $fullscreenHeight AND $fullscreenHeight!=false))) {
$fullscreen = $this->app->make('helper/image')->getThumbnail($fileObject, $fullscreenWidth, $fullscreenHeight, $fullscreenCrop);
$fullscreenLink = $fullscreen->src;
$fullscreenWidth = $fullscreen->width;
$fullscreenHeight = $fullscreen->height;
} else {
$fullscreenLink = $link;
$fullscreenWidth = $width;
$fullscreenHeight = $height;
}
}
// Thumbnail image
if (!empty($options['thumbnail'])) {
$thumbnailWidth = $options['thumbnailWidth'];
$thumbnailHeight = $options['thumbnailHeight'];
$thumbnailCrop = $options['thumbnailCrop'];
if ($fileObject->canEdit() AND (($width > $thumbnailWidth AND $thumbnailWidth!=false) OR ($height > $thumbnailHeight AND $thumbnailHeight!=false))) {
$thumbnail = $this->app->make('helper/image')->getThumbnail($fileObject, $thumbnailWidth, $thumbnailHeight, $thumbnailCrop);
$thumbnailLink = $thumbnail->src;
$thumbnailWidth = $thumbnail->width;
$thumbnailHeight = $thumbnail->height;
} else {
$thumbnailLink = $link;
$thumbnailWidth = $width;
$thumbnailHeight = $height;
}
}
}
}
}
if ($type == 'view') {
// Fields from database
```
--------------------------------
### Display File Basic Information
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/file_set_basic.txt
This snippet shows how to iterate through a list of file objects and display their basic information such as ID, name, URLs, paths, title, description, tags, size, extension, type, mime type, and custom attributes like width, height, and duration.
```php
------------------------------------------------------------
Basic info:
getFileID() . '
';
echo 'File Name: ' . $[[[HANDLE]]]_file->getFileName() . '
';
echo 'Full URL: ' . $[[[HANDLE]]]_file->getURL() . '
';
echo 'Tracked URL: ' . $[[[HANDLE]]]_file->getDownloadURL() . '
';
echo 'Relative Path: ' . $[[[HANDLE]]]_file->getRelativePath() . '
';
echo 'Full Relative Path: ' . $_SERVER['DOCUMENT_ROOT'] . $[[[HANDLE]]]_file->getRelativePath() . '
';
echo 'Title: ' . $[[[HANDLE]]]_file->getTitle() . '
';
echo 'Description: ' . $[[[HANDLE]]]_file->getDescription() . '
';
echo 'Tags (string): ' . $[[[HANDLE]]]_file->getTags() . '
';
echo 'Tags (array): ' . '
';
var_dump($[[[HANDLE]]]_file->getTagsList());
echo '
';
echo 'File Size: ' . $[[[HANDLE]]]_file->getSize() . '
';
echo 'Full File Size: ' . $[[[HANDLE]]]_file->getFullSize() . '
';
echo 'Extension: ' . $[[[HANDLE]]]_file->getExtension() . '
';
echo 'Type: ' . $[[[HANDLE]]]_file->getType() . '
';
echo 'Mime: ' . $[[[HANDLE]]]_file->getMimeType() . '
';
echo 'Display Type: ' . $[[[HANDLE]]]_file->getDisplayType() . '
';
echo 'Generic Type Text: ' . $[[[HANDLE]]]_file->getGenericTypeText() . '
';
echo 'Width: ' . $[[[HANDLE]]]_file->getAttribute('width') . '
';
echo 'Height: ' . $[[[HANDLE]]]_file->getAttribute('height') . '
';
echo 'Duration: ' . $[[[HANDLE]]]_file->getAttribute('duration') . '
';
?>
------------------------------------------------------------
```
--------------------------------
### Generate Thumbnail using Image Helper
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/file_set_basic.txt
This snippet demonstrates how to generate and display a thumbnail for a file using Concrete CMS's image helper. It checks if the file object is valid and if the user has edit permissions before generating the thumbnail and embedding it in an image tag.
```php
canEdit()): ?>
make('helper/image')->getThumbnail($[[[HANDLE]]]_file, 100, 75, true);
?>
```
--------------------------------
### Display Concrete Thumbnail (File Manager Listing)
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/view/file_set_basic.txt
This snippet demonstrates how to display a Concrete CMS thumbnail using a predefined thumbnail type, specifically 'file_manager_listing'. It retrieves the thumbnail URL and dimensions based on the specified type and renders an image tag.
```php
canEdit()): ?>
getThumbnailURL($type->getBaseVersion()); // or $type->getDoubledVersion()
?>
```
--------------------------------
### Return File Data as Entry in ConcreteCMS Block Builder
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/functions/prepare_for_view_image.txt
This PHP code snippet shows how to construct and return an associative array representing file data. It includes the file ID, alternative text, filename, type, relative path, links, and dimensions, formatted for an 'entry' type.
```php
$entry = [];
// Fields from database
$entry[$fileIDFieldName] = $fileID;
$entry[$altFieldName] = $alt;
// Additional data
$entry[$fileIDFieldName.'_filename'] = $filename;
$entry[$fileIDFieldName.'_type'] = $fileType;
$entry[$fileIDFieldName.'_relativePath'] = $relativePath;
$entry[$fileIDFieldName.'_link'] = $link;
$entry[$fileIDFieldName.'_width'] = $width;
$entry[$fileIDFieldName.'_height'] = $height;
$entry[$fileIDFieldName.'_fullscreenLink'] = $fullscreenLink;
$entry[$fileIDFieldName.'_fullscreenWidth'] = $fullscreenWidth;
$entry[$fileIDFieldName.'_fullscreenHeight'] = $fullscreenHeight;
$entry[$fileIDFieldName.'_thumbnailLink'] = $thumbnailLink;
$entry[$fileIDFieldName.'_thumbnailWidth'] = $thumbnailWidth;
$entry[$fileIDFieldName.'_thumbnailHeight'] = $thumbnailHeight;
return $entry;
```
--------------------------------
### Prepare External Link Data (PHP)
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/functions/prepare_for_view_external_link.txt
This PHP function prepares external link data for display or entry. It takes a type ('view' or 'entry') and an array of fields, then formats the link, protocol, text, title, target, and nofollow attributes. It handles different protocol types and constructs the final link URL.
```php
private function prepareForViewExternalLink($type, $fields) {
$keys = array_keys($fields);
$linkFieldName = $keys[0];
$protocolFieldName = $keys[1];
$endingFieldName = $keys[2];
$textFieldName = $keys[3];
$titleFieldName = $keys[4];
$newWindowFieldName = $keys[5];
$noFollowFieldName = $keys[6];
$link = $fields[$linkFieldName];
$protocol = $fields[$protocolFieldName];
$ending = $fields[$endingFieldName];
$text = $fields[$textFieldName];
$title = $fields[$titleFieldName];
$newWindow = !empty($fields[$newWindowFieldName]) ? 'target="_blank"' : '';
$noFollow = !empty($fields[$noFollowFieldName]) ? 'rel="nofollow"' : '';
if ($type == 'view') {
// Fields from database
$this->set($linkFieldName, $link);
$this->set($protocolFieldName, $protocol);
$this->set($endingFieldName, $ending);
$this->set($textFieldName, $text);
$this->set($titleFieldName, $title);
$this->set($newWindowFieldName, $newWindow);
$this->set($noFollowFieldName, $noFollow);
// Additional data
if (!empty($link) AND in_array($protocol, ['http://', 'https://'])) {
$link = $protocol.$link;
}
if (!empty($link) AND $protocol=='BASE_URL') {
$separator = '';
if (substr($link, 0, 1)!='/') {
$separator = '/';
}
$link = BASE_URL.$separator.$link;
}
if (!empty($link) AND $protocol=='CURRENT_PAGE') {
$separator = '';
if (substr($link, 0, 1)!='/') {
$separator = '/';
}
$link = Page::getCurrentPage()->getCollectionLink().$separator.$link;
}
$this->set($linkFieldName.'_link', $link);
$this->set($linkFieldName.'_link_type', 'external_link');
} elseif ($type == 'entry') {
$entry = [];
// Fields from database
$entry[$linkFieldName] = $link;
$entry[$protocolFieldName] = $protocol;
$entry[$endingFieldName] = $ending;
$entry[$textFieldName] = $text;
$entry[$titleFieldName] = $title;
$entry[$newWindowFieldName] = $newWindow;
$entry[$noFollowFieldName] = $noFollow;
// Additional data
if (!empty($link) AND in_array($protocol, ['http://', 'https://'])) {
$link = $protocol.$link;
}
if (!empty($link) AND $protocol=='BASE_URL') {
$separator = '';
if (substr($link, 0, 1)!='/') {
$separator = '/';
}
$link = BASE_URL.$separator.$link;
}
if (!empty($link) AND $protocol=='CURRENT_PAGE') {
$separator = '';
if (substr($link, 0, 1)!='/') {
$separator = '/';
}
$link = Page::getCurrentPage()->getCollectionLink().$separator.$link;
}
$entry[$linkFieldName.'_link'] = $link;
$entry[$linkFieldName.'_link_type'] = 'external_link';
return $entry;
}
}
```
--------------------------------
### Prepare File Manager Link Data for View/Entry (PHP)
Source: https://github.com/parasek/concretecms-block-builder/blob/develop/generator_templates/functions/prepare_for_view_link_from_file_manager.txt
This PHP function takes file manager field data and prepares it for either direct view rendering or for structured entry data. It retrieves file objects, generates links, and sets necessary view variables or returns an entry array.
```php
private function prepareForViewLinkFromFileManager($type, $fields) {
$keys = array_keys($fields);
$fileIDFieldName = $keys[0];
$endingFieldName = $keys[1];
$textFieldName = $keys[2];
$titleFieldName = $keys[3];
$newWindowFieldName = $keys[4];
$noFollowFieldName = $keys[5];
$fileID = $fields[$fileIDFieldName];
$ending = $fields[$endingFieldName];
$text = $fields[$textFieldName];
$title = $fields[$titleFieldName];
$newWindow = !empty($fields[$newWindowFieldName]) ? 'target="_blank"' : '';
$noFollow = !empty($fields[$noFollowFieldName]) ? 'rel="nofollow"' : '';
$fileObject = false;
$filename = '';
$link = '';
if (!empty($fileID)) {
$fileObject = File::getByID($fileID);
if (is_object($fileObject)) {
$link = $fileObject->getURL();
$filename = $fileObject->getFileName();
}
}
if ($type == 'view') {
// Fields from database
$this->set($fileIDFieldName, $fileID);
$this->set($endingFieldName, $ending);
$this->set($textFieldName, $text);
$this->set($titleFieldName, $title);
$this->set($newWindowFieldName, $newWindow);
$this->set($noFollowFieldName, $noFollow);
// Additional data
$this->set($fileIDFieldName.'_object', $fileObject);
$this->set($fileIDFieldName.'_filename', $filename);
$this->set($fileIDFieldName.'_link', $link);
$this->set($fileIDFieldName.'_link_type', 'link_from_file_manager');
} elseif ($type == 'entry') {
$entry = [];
// Fields from database
$entry[$fileIDFieldName] = $fileID;
$entry[$endingFieldName] = $ending;
$entry[$textFieldName] = $text;
$entry[$titleFieldName] = $title;
$entry[$newWindowFieldName] = $newWindow;
$entry[$noFollowFieldName] = $noFollow;
// Additional data
// $entry[$fileIDFieldName.'_object'] = $fileObject;
$entry[$fileIDFieldName.'_filename'] = $filename;
$entry[$fileIDFieldName.'_link'] = $link;
$entry[$fileIDFieldName.'_link_type'] = 'link_from_file_manager';
return $entry;
}
}
```