@@ -1332,12 +1332,27 @@ static QVariant fcnFeature( const QVariantList &, const QgsExpressionContext *co
1332
1332
1333
1333
return context->feature ();
1334
1334
}
1335
- static QVariant fcnAttribute ( const QVariantList &values, const QgsExpressionContext *, QgsExpression *parent, const QgsExpressionNodeFunction * )
1335
+ static QVariant fcnAttribute ( const QVariantList &values, const QgsExpressionContext *context , QgsExpression *parent, const QgsExpressionNodeFunction * )
1336
1336
{
1337
- QgsFeature feat = QgsExpressionUtils::getFeature ( values.at ( 0 ), parent );
1338
- QString attr = QgsExpressionUtils::getStringValue ( values.at ( 1 ), parent );
1337
+ QgsFeature feature;
1338
+ QString attr;
1339
+ if ( values.size () == 1 )
1340
+ {
1341
+ attr = QgsExpressionUtils::getStringValue ( values.at ( 0 ), parent );
1342
+ feature = context->feature ();
1343
+ }
1344
+ else if ( values.size () == 2 )
1345
+ {
1346
+ feature = QgsExpressionUtils::getFeature ( values.at ( 0 ), parent );
1347
+ attr = QgsExpressionUtils::getStringValue ( values.at ( 1 ), parent );
1348
+ }
1349
+ else
1350
+ {
1351
+ parent->setEvalErrorString ( QObject::tr ( " Function `attribute` requires one or two parameters. %1 given." ).arg ( values.length () ) );
1352
+ return QVariant ();
1353
+ }
1339
1354
1340
- return feat .attribute ( attr );
1355
+ return feature .attribute ( attr );
1341
1356
}
1342
1357
1343
1358
static QVariant fcnIsSelected ( const QVariantList &values, const QgsExpressionContext *context, QgsExpression *parent, const QgsExpressionNodeFunction * )
@@ -5319,10 +5334,30 @@ const QList<QgsExpressionFunction *> &QgsExpression::Functions()
5319
5334
5320
5335
sFunctions << evalFunc;
5321
5336
5337
+ QgsStaticExpressionFunction *attributeFunc = new QgsStaticExpressionFunction ( QStringLiteral ( " attribute" ), -1 , fcnAttribute, QStringLiteral ( " Record and Attributes" ), QString (), false , QSet<QString>() << QgsFeatureRequest::ALL_ATTRIBUTES );
5338
+ attributeFunc->setIsStaticFunction (
5339
+ []( const QgsExpressionNodeFunction * node, QgsExpression * parent, const QgsExpressionContext * context )
5340
+ {
5341
+ const QList< QgsExpressionNode *> argList = node->args ()->list ();
5342
+ for ( QgsExpressionNode *argNode : argList )
5343
+ {
5344
+ if ( !argNode->isStatic ( parent, context ) )
5345
+ return false ;
5346
+ }
5347
+
5348
+ if ( node->args ()->count () == 1 )
5349
+ {
5350
+ // not static -- this is the variant which uses the current feature taken direct from the expression context
5351
+ return false ;
5352
+ }
5353
+
5354
+ return true ;
5355
+ } );
5356
+ sFunctions << attributeFunc;
5357
+
5322
5358
sFunctions
5323
5359
<< new QgsStaticExpressionFunction ( QStringLiteral ( " env" ), 1 , fcnEnvVar, QStringLiteral ( " General" ), QString () )
5324
5360
<< new QgsWithVariableExpressionFunction ()
5325
- << new QgsStaticExpressionFunction ( QStringLiteral ( " attribute" ), 2 , fcnAttribute, QStringLiteral ( " Record and Attributes" ), QString (), false , QSet<QString>() << QgsFeatureRequest::ALL_ATTRIBUTES )
5326
5361
<< new QgsStaticExpressionFunction ( QStringLiteral ( " raster_value" ), QgsExpressionFunction::ParameterList () << QgsExpressionFunction::Parameter ( QStringLiteral ( " layer" ) ) << QgsExpressionFunction::Parameter ( QStringLiteral ( " band" ) ) << QgsExpressionFunction::Parameter ( QStringLiteral ( " point" ) ), fcnRasterValue, QStringLiteral ( " Rasters" ) )
5327
5362
5328
5363
// functions for arrays
0 commit comments