Learn more about CSS Classes
*/
public string $cssClasses;
/**
* @label With Gutenberg Block
* @instructions If checked, a separate gutenberg block for this view will be available. Read more
* @a-pro The field must be not required or have default value!
* @a-acf-pro ACF PRO version is necessary for this feature
*/
public bool $isHasGutenbergBlock;
/**
* @item \org\wplake\acf_views\AcfGroups\Item
* @var Item[]
* @label Fields
* @instructions Fields of this view. Tip : hover mouse on the field number column and drag to reorder
* @button_label Add Field
* @a-no-accordion 1
*/
public array $items;
/**
* @a-type tab
* @label Markup
*/
public bool $markupTab;
/**
* @a-type textarea
* @new_lines br
* @label Markup Preview
* @instructions Output preview of HTML markup generated. Important! Publish or Update your view to see the latest markup
*/
public string $markup;
/**
* @a-type textarea
* @label Custom Markup
* @instructions Warning : for users familiar with HTML. Write your own HTML markup with full control over how your view will look. You can copy the Markup field output and make your changes.
Make sure you've kept all variables (look like '$name$') and wrappers (look like HTML comments), otherwise field values won't be inserted.
Important! This field will not be updated automatically when you add or remove fields, so you have to update this field manually to reflect the new changes (you could see the Markup field for assistance). Read more
* @a-pro The field must be not required or have default value!
*/
public string $customMarkup;
/**
* @a-type textarea
* @label Custom Markup Variables
* @instructions You can add custom variables to the custom markup using this PHP code snippet.
The snippet must return an associative array of values, where keys are variable names. These variables will be available in the custom markup within brackets, like '{VARIABLE_NAME}'.
In the snippet pre-defined following variables : '$_objectId' (current data post), '$_viewId' (current view id),'$_fields' (an associative field values array, where keys are field identifiers). Read more
* @default_value Magic shortcuts are available :
'#view' will be replaced with '.acf-view--id--X'.
'#view__' will be replaced with '.acf-view--id--X .acf-view__'. It means you can use '#view__row' and it'll be replaced with '.acf-view--id--X .acf-view__row'
*/
public string $cssCode;
/**
* @a-type textarea
* @label JS Code
* @instructions Add your own Javascript to your view. This will be added within <script></script> tags ONLY to pages that have this view and also will be wrapped into an anonymous function to avoid name conflicts
*/
public string $jsCode;
/**
* @a-type tab
* @label Preview
*/
public bool $previewTab;
/**
* @a-type post_object
* @return_format 1
* @allow_null 1
* @label Preview Object
* @instructions Select a data object (which field values will be used) and press the 'Update' button to see the final markup in the preview
*/
public int $previewPost;
/**
* @label Preview
* @instructions Here you can see the preview of the view and play with CSS rules. Important! Press the 'Update' button after changes to see the latest markup here.
Your changes to the preview won't be applied to the view automatically, if you want to keep them copy amended CSS to the 'CSS Code' field and press the 'Update' button.
Note : except the view styles the preview contains also styles of your homepage to give the preview the same look as the view will have on your front (some differences are still possible).
* @placeholder Loading... Please wait a few seconds
* @disabled 1
*/
public string $preview;
private array $fieldsMeta;
public function __construct(CreatorInterface $creator)
{
parent::__construct($creator);
$this->fieldsMeta = [];
}
public static function getGroupInfo(): array
{
return array_merge(parent::getGroupInfo(), [
'title' => 'View settings',
]);
}
public function setFieldsMeta(array $fieldsMeta = [], bool $isForce = false): void
{
if ($fieldsMeta ||
$isForce) {
$this->fieldsMeta = $fieldsMeta;
return;
}
foreach ($this->items as $item) {
$fieldId = $item->field->getAcfFieldId();
$this->fieldsMeta[$fieldId] = new FieldMeta($fieldId);
}
}
/**
* @return FieldMeta[]
*/
public function getFieldsMeta(): array
{
return $this->fieldsMeta;
}
public function getCssCode(bool $isMinify = true, bool $isPreview = false): string
{
$cssCode = $this->cssCode;
if ($isMinify) {
// remove all CSS comments
$cssCode = preg_replace('|\/\*(.?)+\*\/|', '', $cssCode);
// 'minify' CSS
$cssCode = str_replace(["\t", "\n", "\r"], '', $cssCode);
// magic shortcuts
$cssCode = str_replace('#view ', sprintf('.acf-view--id--%s ', $this->getSource()), $cssCode);
$cssCode = str_replace('#view{', sprintf('.acf-view--id--%s{', $this->getSource()), $cssCode);
$cssCode = str_replace('#view__', sprintf('.acf-view--id--%s .acf-view__', $this->getSource()), $cssCode);
$cssCode = trim($cssCode);
} elseif ($isPreview) {
$cssCode = str_replace('#view__', sprintf('#view .acf-view__', $this->getSource()), $cssCode);
}
return $cssCode;
}
public function isHasMapField(): bool
{
$fieldsMeta = $this->getFieldsMeta();
foreach ($this->items as $item) {
$fieldMeta = $fieldsMeta[$item->field->getAcfFieldId()] ?? null;
if (! $fieldMeta) {
continue;
}
if ('google_map' === $fieldMeta->getType()) {
return true;
}
$repeaterFieldsMeta = $item->getRepeaterFieldsMeta();
foreach ($repeaterFieldsMeta as $repeaterFieldMeta) {
if ('google_map' === $repeaterFieldMeta->getType()) {
return true;
}
}
}
return false;
}
public function saveToPostContent(array $postFields = []): bool
{
$isHasGutenberg = $this->isHasGutenbergBlock ?
static::POST_VALUE_IS_HAS_GUTENBERG :
'';
$postFields = array_merge($postFields, [
static::POST_FIELD_IS_HAS_GUTENBERG => $isHasGutenberg,
]);
return parent::saveToPostContent($postFields);
}
}