Contao\StringUtil::srcToInsertTag PHP Метод

srcToInsertTag() публичный статический Метод

Convert file paths inside "src" attributes to insert tags
public static srcToInsertTag ( string $data ) : string
$data string The markup string
Результат string The markup with file paths converted to insert tags
    public static function srcToInsertTag($data)
    {
        $return = '';
        $paths = preg_split('/((src|href)="([^"]+)")/i', $data, -1, PREG_SPLIT_DELIM_CAPTURE);
        for ($i = 0, $c = count($paths); $i < $c; $i = $i + 4) {
            $return .= $paths[$i];
            if (!isset($paths[$i + 1])) {
                continue;
            }
            $file = \FilesModel::findByPath($paths[$i + 3]);
            if ($file !== null) {
                $return .= $paths[$i + 2] . '="{{file::' . static::binToUuid($file->uuid) . '}}"';
            } else {
                $return .= $paths[$i + 2] . '="' . $paths[$i + 3] . '"';
            }
        }
        return $return;
    }

Usage Example

 /**
  * Convert file source to uuid before save the model.
  * After convert this is an insert tag {{file::uuid}}.
  *
  * @param EncodePropertyValueFromWidgetEvent $event The event to handle.
  *
  * @return void
  */
 public function convertFileSourceToUuid(EncodePropertyValueFromWidgetEvent $event)
 {
     $environment = $event->getEnvironment();
     $dataDefinition = $environment->getDataDefinition();
     $propertiesDefinition = $dataDefinition->getPropertiesDefinition();
     $property = $propertiesDefinition->getProperty($event->getProperty());
     if (!array_key_exists('rte', $property->getExtra()) || strpos($property->getExtra()['rte'], 'tiny') !== 0) {
         return;
     }
     $event->setValue(StringUtil::srcToInsertTag($event->getValue()));
 }
All Usage Examples Of Contao\StringUtil::srcToInsertTag