Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
feeds
Manage
Activity
Members
Labels
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Deploy
Releases
Model registry
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
drupal.org
feeds
Commits
464f857a
Commit
464f857a
authored
12 years ago
by
Chris Leppanen
Browse files
Options
Downloads
Patches
Plain Diff
Move text and numeric mappers to their own include.
parent
53dfe8b3
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
mappers/field.inc
+0
-130
0 additions, 130 deletions
mappers/field.inc
mappers/number.inc
+74
-0
74 additions, 0 deletions
mappers/number.inc
mappers/text.inc
+78
-0
78 additions, 0 deletions
mappers/text.inc
with
152 additions
and
130 deletions
mappers/field.inc
deleted
100644 → 0
+
0
−
130
View file @
53dfe8b3
<?php
/**
* @file
* On behalf implementation of Feeds mapping API for field.module.
*
* Does actually not include mappers for field types defined in fields module
* (because there aren't any) but mappers for all fields that contain their
* value simply in $entity->fieldname['und'][$i]['value'].
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsNodeProcessor::getMappingTargets().
*/
function
field_feeds_processor_targets_alter
(
&
$targets
,
$entity_type
,
$bundle_name
)
{
$numeric_types
=
array
(
'list_integer'
,
'list_float'
,
'list_boolean'
,
'number_integer'
,
'number_decimal'
,
'number_float'
,
);
$string_types
=
array
(
'list_text'
,
'text'
,
'text_long'
,
'text_with_summary'
,
);
foreach
(
field_info_instances
(
$entity_type
,
$bundle_name
)
as
$name
=>
$instance
)
{
$info
=
field_info_field
(
$name
);
unset
(
$callback
);
if
(
in_array
(
$info
[
'type'
],
$numeric_types
))
{
$callback
=
'field_feeds_set_target_numeric'
;
}
if
(
in_array
(
$info
[
'type'
],
$string_types
))
{
$callback
=
'field_feeds_set_target_text'
;
}
if
(
isset
(
$callback
))
{
$targets
[
$name
]
=
array
(
'name'
=>
check_plain
(
$instance
[
'label'
]),
'callback'
=>
$callback
,
'description'
=>
t
(
'The @label field of the node.'
,
array
(
'@label'
=>
$instance
[
'label'
])),
);
}
}
}
/**
* Callback for mapping numerics.
*
* Ensure that $value is a numeric to avoid database errors.
*/
function
field_feeds_set_target_numeric
(
$source
,
$entity
,
$target
,
$value
)
{
if
(
!
is_array
(
$value
))
{
$value
=
array
(
$value
);
}
foreach
(
$value
as
$k
=>
$v
)
{
if
(
!
is_numeric
(
$v
))
{
unset
(
$value
[
$k
]);
}
}
_field_feeds_set_target
(
$source
,
$entity
,
$target
,
$value
,
FALSE
);
}
/**
* Callback for mapping text fields.
*/
function
field_feeds_set_target_text
(
$source
,
$entity
,
$target
,
$value
)
{
if
(
!
is_array
(
$value
))
{
$value
=
array
(
$value
);
}
_field_feeds_set_target
(
$source
,
$entity
,
$target
,
$value
,
TRUE
);
}
/**
* Helper for mapping.
*
* When the callback is invoked, $target contains the name of the field the
* user has decided to map to and $value contains the value of the feed item
* element the user has picked as a source.
*
* @param $source
* A FeedsSource object.
* @param $entity
* The entity to map to.
* @param $target
* The target key on $entity to map to.
* @param $value
* The value to map. MUST be an array.
* @param $input_format
* TRUE if an input format should be applied.
*/
function
_field_feeds_set_target
(
$source
,
$entity
,
$target
,
$value
,
$input_format
=
FALSE
)
{
if
(
empty
(
$value
))
{
return
;
}
if
(
$input_format
)
{
if
(
isset
(
$source
->
importer
->
processor
->
config
[
'input_format'
]))
{
$format
=
$source
->
importer
->
processor
->
config
[
'input_format'
];
}
}
$info
=
field_info_field
(
$target
);
// Iterate over all values.
$i
=
0
;
$field
=
isset
(
$entity
->
$target
)
?
$entity
->
$target
:
array
();
foreach
(
$value
as
$v
)
{
if
(
is_object
(
$v
)
&&
(
$v
instanceof
FeedsElement
))
{
$v
=
$v
->
getValue
();
}
if
(
!
is_array
(
$v
)
&&
!
is_object
(
$v
))
{
$field
[
'und'
][
$i
][
'value'
]
=
$v
;
}
if
(
$input_format
)
{
if
(
isset
(
$format
))
{
$field
[
'und'
][
$i
][
'format'
]
=
$format
;
}
}
if
(
$info
[
'cardinality'
]
==
1
)
{
break
;
}
$i
++
;
}
$entity
->
{
$target
}
=
$field
;
}
This diff is collapsed.
Click to expand it.
mappers/number.inc
0 → 100644
+
74
−
0
View file @
464f857a
<?php
/**
* @file
* On behalf implementation of Feeds mapping API for number.module.
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsProcessor::getMappingTargets()
*/
function
number_feeds_processor_targets_alter
(
&
$targets
,
$entity_type
,
$bundle_name
)
{
$numeric_types
=
array
(
'list_integer'
,
'list_float'
,
'list_boolean'
,
'number_integer'
,
'number_decimal'
,
'number_float'
,
);
foreach
(
field_info_instances
(
$entity_type
,
$bundle_name
)
as
$name
=>
$instance
)
{
$info
=
field_info_field
(
$name
);
if
(
in_array
(
$info
[
'type'
],
$numeric_types
))
{
$targets
[
$name
]
=
array
(
'name'
=>
check_plain
(
$instance
[
'label'
]),
'callback'
=>
'number_feeds_set_target'
,
'description'
=>
t
(
'The @label field of the entity.'
,
array
(
'@label'
=>
$instance
[
'label'
])),
);
}
}
}
/**
* Callback for mapping numerics.
*
* Ensure that $value is a numeric to avoid database errors.
*/
function
number_feeds_set_target
(
$source
,
$entity
,
$target
,
$value
)
{
if
(
empty
(
$value
))
{
return
;
}
if
(
!
is_array
(
$value
))
{
$value
=
array
(
$value
);
}
$info
=
field_info_field
(
$target
);
// Iterate over all values.
$field
=
isset
(
$entity
->
$target
)
?
$entity
->
$target
:
array
(
'und'
=>
array
());
// Allow for multiple mappings to the same target.
$delta
=
count
(
$field
[
'und'
]);
foreach
(
$value
as
$v
)
{
if
(
$info
[
'cardinality'
]
==
$delta
)
{
break
;
}
if
(
is_object
(
$v
)
&&
(
$v
instanceof
FeedsElement
))
{
$v
=
$v
->
getValue
();
}
if
(
is_numeric
(
$v
))
{
$field
[
'und'
][
$delta
][
'value'
]
=
$v
;
$delta
++
;
}
}
$entity
->
$target
=
$field
;
}
This diff is collapsed.
Click to expand it.
mappers/text.inc
0 → 100644
+
78
−
0
View file @
464f857a
<?php
/**
* @file
* On behalf implementation of Feeds mapping API for text.module.
*/
/**
* Implements hook_feeds_processor_targets_alter().
*
* @see FeedsProcessor::getMappingTargets()
*/
function
text_feeds_processor_targets_alter
(
&
$targets
,
$entity_type
,
$bundle_name
)
{
$text_types
=
array
(
'list_text'
,
'text'
,
'text_long'
,
'text_with_summary'
,
);
foreach
(
field_info_instances
(
$entity_type
,
$bundle_name
)
as
$name
=>
$instance
)
{
$info
=
field_info_field
(
$name
);
if
(
in_array
(
$info
[
'type'
],
$text_types
))
{
$targets
[
$name
]
=
array
(
'name'
=>
check_plain
(
$instance
[
'label'
]),
'callback'
=>
'field_feeds_set_target_text'
,
'description'
=>
t
(
'The @label field of the entity.'
,
array
(
'@label'
=>
$instance
[
'label'
])),
);
}
}
}
/**
* Callback for mapping text fields.
*/
function
field_feeds_set_target_text
(
$source
,
$entity
,
$target
,
$value
)
{
if
(
empty
(
$value
))
{
return
;
}
if
(
!
is_array
(
$value
))
{
$value
=
array
(
$value
);
}
if
(
isset
(
$source
->
importer
->
processor
->
config
[
'input_format'
]))
{
$format
=
$source
->
importer
->
processor
->
config
[
'input_format'
];
}
$info
=
field_info_field
(
$target
);
// Iterate over all values.
$field
=
isset
(
$entity
->
$target
)
?
$entity
->
$target
:
array
(
'und'
=>
array
());
// Allow for multiple mappings to the same target.
$delta
=
count
(
$field
[
'und'
]);
foreach
(
$value
as
$v
)
{
if
(
$info
[
'cardinality'
]
==
$delta
)
{
break
;
}
if
(
is_object
(
$v
)
&&
(
$v
instanceof
FeedsElement
))
{
$v
=
$v
->
getValue
();
}
if
(
is_scalar
(
$v
))
{
$field
[
'und'
][
$delta
][
'value'
]
=
$v
;
$delta
++
;
if
(
isset
(
$format
))
{
$field
[
'und'
][
$delta
][
'format'
]
=
$format
;
}
}
}
$entity
->
$target
=
$field
;
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment