Skip to content

Instantly share code, notes, and snippets.

View fago's full-sized avatar

Wolfgang Ziegler fago

View GitHub Profile
@fago
fago / reaction rule conditions
Created February 17, 2010 16:48
reaction rule
$rule = rules_reaction_rule();
$rule->event('node_insert')
->condition(rules_or()->condition(rules_and()))
->condition(rules_condition('data_is', array('data:select' => 'node:status', 'value' => TRUE))->negate())
->condition('data_is', array('data:select' => 'node:type', 'op' => 'IN', 'value' => array('page', 'story')))
->condition('data_is', array('data:select' => 'node:type', 'op' => 'IN', 'value' => array('page', 'story')));
$rule->save('test');
@fago
fago / example rule with or
Created February 24, 2010 10:42
reaction rule example
$rule = rules_reaction_rule();
$rule->event('node_insert');
$rule->condition('data_is', array('data:select' => 'node:type', 'op' => 'IN', 'value' => array('product')));
$rule->condition(rules_or()
->condition('data_is', array('data:select' => 'node:product:0:type', 'value' => 'shirt'))
->condition('data_is', array('data:select' => 'node:product:author:uid', 'value' => 1))
);
$rule->action('data_set', array('data' => 'node:status', 'value' => 1));
$rule->save();
@fago
fago / open calais
Created April 2, 2010 15:55
open calais remote site
$remote = new RulesWebRemote();
$remote->name = 'calais';
$remote->label = 'Open calais';
$remote->url = 'http://api.opencalais.com/enlighten/?wsdl';
$remote->type = 'soap';
// Add info about the SOAP service, which the user has to provide via the UI.
// We need to define the "Enlighten" complex data type, which the operation
// takes as parameter.
$remote->settings['data types']['Enlighten'] = array(
@fago
fago / gist:4055409
Created November 11, 2012 16:22
test comment property access
$comments = comment_load_multiple();
for ($i =0; $i<1; $i++) {
foreach ($comments as $comment)
foreach ($comment as $name => $property)
foreach ($property as $item)
foreach ($item as $value)
$value->getType();
}
@fago
fago / gist:4169427
Created November 29, 2012 14:25
defintion translation speed test
$iterations = 10000;
timer_start('test');
for ($i = 0; $i < $iterations; $i++) {
$d = serialize($definitions);
$e = unserialize($d);
}
timer_stop('test');
echo timer_read('test')."ms for $iterations runs. \n";
echo (timer_read('test') / $iterations) ."ms per run";
@fago
fago / gist:4747199
Created February 9, 2013 21:31
executable validation
/**
* Implements \Drupal\Core\Executable\ExecutableInterface::valdidate().
*/
public function valdidate() {
$violations = new \Symfony\Component\Validator\ConstraintViolationList();
if ($definitions = $this->getContextDefinitions()) {
foreach ($definitions as $key => $definition) {
// Validate any set values.
if (isset($this->context[$key])) {
print_r($typed_data->getPropertyDefintions());
foreach ($typed_data as $name => $property) {
// Property is type data as well
echo $property->getType();
// introspect property? check it's interface and continue
}
protected function assertIterator($entity_type) {
$entity = $this->createTestEntity($entity_type);
foreach ($entity as $name => $field) {
$this->assertTrue($field instanceof FieldInterface, $entity_type . ": Field $name implements interface.");
foreach ($field as $delta => $item) {
$this->assertTrue($field[0] instanceof FieldItemInterface, $entity_type . ": Item $delta of field $name implements interface.");
foreach ($item as $value_name => $value_property) {
@fago
fago / gist:4968056
Created February 16, 2013 18:26
$plugin->setContextValue()
/**
* Implements \Drupal\Component\Plugin\ContextAwarePluginInterface::setContextValue().
*/
public function setContextValue($key, $value) {
$context_definition = $this->getContextDefinition($key);
$this->context[$key] = new Context($context_definition);
$this->context[$key]->setValue($value);
if ($this->context[$key]->validate($value)->count() > 0) {
throw new PluginException("The provided value for $key is not valid.");
@fago
fago / gist:5344541
Created April 9, 2013 10:00
storage stuff
class DatabaseStorageController implements EntityStorageControllerInterface, ExtensibleStorageInterface {
protected $class = 'ExtensibleDBStorageHelper';
protected function getExtensibleStorageHelper() {
...
}
function save() {