mirror of
https://github.com/processwire/processwire.git
synced 2025-08-09 16:26:59 +02:00
Add new convenience method Field::getContext();
This commit is contained in:
@@ -36,6 +36,7 @@
|
||||
* @property int|null $paginationLimit Used by paginated WireArray values to indicate limit to use during load. #pw-internal
|
||||
* @property array $allowContexts Names of settings that are custom configured to be allowed for context. #pw-group-properties
|
||||
* @property bool|int|null $flagUnique Non-empty value indicates request for, or presence of, Field::flagUnique flag. #pw-internal
|
||||
* @property Fieldgroup|null $_contextFieldgroup Fieldgroup field is in context for or null if not in context. #pw-internal
|
||||
*
|
||||
* Common Inputfield properties that Field objects store:
|
||||
* @property int|bool|null $required Whether or not this field is required during input #pw-group-properties
|
||||
@@ -673,6 +674,43 @@ class Field extends WireData implements Saveable, Exportable {
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get this field in context of a Page/Template
|
||||
*
|
||||
* #pw-group-retrieval
|
||||
*
|
||||
* @param Page|Template|Fieldgroup|string $for Specify Page, Template, or template name string
|
||||
* @param string $namespace Optional namespace (internal use)
|
||||
* @param bool $has Return boolean rather than Field to check if context exists? (default=false)
|
||||
* @return Field|bool
|
||||
* @since 3.0.162
|
||||
* @see Fieldgroup::getFieldContext(), Field::hasContext()
|
||||
*
|
||||
*/
|
||||
public function getContext($for, $namespace = '', $has = false) {
|
||||
/** @var Fieldgroup|null $fieldgroup */
|
||||
$fieldgroup = null;
|
||||
if(is_string($for)) {
|
||||
$for = $this->wire()->templates->get($for);
|
||||
}
|
||||
if($for instanceof Page) {
|
||||
/** @var Page $context */
|
||||
$template = $for instanceof NullPage ? null : $for->template;
|
||||
if(!$template) throw new WireException('Page must have template to get context');
|
||||
$fieldgroup = $template->fieldgroup;
|
||||
} else if($for instanceof Template) {
|
||||
/** @var Template $context */
|
||||
$fieldgroup = $for->fieldgroup;
|
||||
} else if($for instanceof Fieldgroup) {
|
||||
$fieldgroup = $for;
|
||||
}
|
||||
if(!$fieldgroup) throw new WireException('Cannot get Fieldgroup for field context');
|
||||
|
||||
if($has) return $fieldgroup->hasFieldContext($this->id, $namespace);
|
||||
|
||||
return $fieldgroup->getFieldContext($this->id, $namespace);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the roles that are allowed to view or edit this field on pages.
|
||||
*
|
||||
|
@@ -266,6 +266,7 @@ class Fieldgroup extends WireArray implements Saveable, Exportable, HasLookupIte
|
||||
|
||||
if($useFieldgroupContext && $value) {
|
||||
$value->flags = $value->flags | Field::flagFieldgroupContext;
|
||||
$value->setQuietly('_contextFieldgroup', $this);
|
||||
}
|
||||
|
||||
return $value;
|
||||
|
@@ -424,7 +424,7 @@ class Fields extends WireSaveableItems {
|
||||
|
||||
$field_id = (int) $field->id;
|
||||
$fieldgroup_id = (int) $fieldgroup->id;
|
||||
$database = $this->wire('database');
|
||||
$database = $this->wire()->database;
|
||||
|
||||
$newValues = $field->getArray();
|
||||
$oldValues = $fieldOriginal->getArray();
|
||||
@@ -502,13 +502,12 @@ class Fields extends WireSaveableItems {
|
||||
// if there is something in data, then JSON encode it. If it's empty then make it null.
|
||||
$data = count($data) ? wireEncodeJSON($data, true) : null;
|
||||
|
||||
if(is_null($data)) {
|
||||
$data = 'NULL';
|
||||
$query = $database->prepare('UPDATE fieldgroups_fields SET data=:data WHERE fields_id=:field_id AND fieldgroups_id=:fieldgroup_id');
|
||||
if(empty($data)) {
|
||||
$query->bindValue(':data', null, \PDO::PARAM_NULL);
|
||||
} else {
|
||||
$data = "'" . $this->wire('database')->escapeStr($data) . "'";
|
||||
$query->bindValue(':data', $data, \PDO::PARAM_STR);
|
||||
}
|
||||
|
||||
$query = $database->prepare("UPDATE fieldgroups_fields SET data=$data WHERE fields_id=:field_id AND fieldgroups_id=:fieldgroup_id"); // QA
|
||||
$query->bindValue(':field_id', $field_id, \PDO::PARAM_INT);
|
||||
$query->bindValue(':fieldgroup_id', $fieldgroup_id, \PDO::PARAM_INT);
|
||||
$result = $query->execute();
|
||||
|
Reference in New Issue
Block a user