acf = $acf; $this->view = $view; $this->dataPost = $dataPost; $this->pageId = $pageId; $this->html = $markup; $this->twig = $twig; $this->fields = $fields; } protected function getTwigArgsForVariable( Item $item, FieldMeta $fieldMeta, int $viewId, $notFormattedValue, $formattedValue ): array { if (in_array($fieldMeta->getType(), ['repeater', 'group',], true)) { return []; } $twigArgs = $this->fields->getFieldTwigArgs( $item->field, $fieldMeta, $viewId, $notFormattedValue, $formattedValue ); return [ $item->field->getTwigFieldId() => array_merge( $twigArgs, [ 'label' => $item->field->label, ] ), ]; } protected function renderTwig(array $twigVariables, array $fieldValues): void { $this->html = $this->twig->render($this->view->getSource(), $this->html, $twigVariables); } public function insertFields(bool $isMinifyMarkup = true): array { if ($isMinifyMarkup) { // remove special symbols that used in the markup for a preview // exactly here, before the fields are inserted, to avoid affecting them $this->html = str_replace(["\t", "\n", "\r"], '', $this->html); } $fieldValues = []; // internal variables $twigVariables = [ '_view' => [ 'classes' => $this->view->cssClasses ? $this->view->cssClasses . ' ' : '', 'id' => $this->view->getSource(), 'object_id' => strval($this->dataPost->getId()), ], ]; $fieldsMeta = $this->view->getFieldsMeta(); // e.g. already filled for cache/tests if (!$fieldsMeta) { $this->view->setFieldsMeta(); $fieldsMeta = $this->view->getFieldsMeta(); } foreach ($this->view->items as $item) { $fieldMeta = $fieldsMeta[$item->field->getAcfFieldId()]; // for IDE if (!$fieldMeta instanceof FieldMeta) { continue; } list($notFormattedFieldValue, $formattedFieldValue) = $this->dataPost->getFieldValue( $fieldMeta->getFieldId() ); // 1. default value from our plugin. Note: custom field types don't support default values if (!$notFormattedFieldValue && !$fieldMeta->isCustomType()) { $notFormattedFieldValue = $formattedFieldValue = $item->field->defaultValue; } // 2. default value from ACF. Note: custom field types don't support default values if (!$notFormattedFieldValue && !$fieldMeta->isCustomType()) { $notFormattedFieldValue = $formattedFieldValue = $fieldMeta->getDefaultValue(); } $fieldValues[$item->field->id] = $formattedFieldValue; $twigVariables = array_merge( $twigVariables, $this->getTwigArgsForVariable( $item, $fieldMeta, $this->view->getSource(), $notFormattedFieldValue, $formattedFieldValue ) ); } $this->renderTwig($twigVariables, $fieldValues); return $fieldValues; } public function getHTML(): string { return $this->html; } }